Skip to content

Commit

Permalink
Re-group HTTP steps and related logic (#5243)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored Jul 26, 2024
1 parent 46d9b4a commit 681c156
Show file tree
Hide file tree
Showing 49 changed files with 384 additions and 451 deletions.
24 changes: 24 additions & 0 deletions docs/modules/plugins/pages/plugin-rest-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,30 @@ Then HTTP resources are valid:
| https://vividus-test-site-a92k.onrender.com/img/vividus.png |
----

=== Validate redirects

Check that all URLs from xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] redirect to proper pages with correct redirects number. Validation fails if either actual final URL or number of redirects do not match the expected values.

NOTE: The step throws the error in case if https://developer.mozilla.org/en-US/docs/Web/HTTP/Status[HTTP response status code] of checked URL out of range 200-207.

[source,gherkin]
----
Then I validate HTTP redirects:$expectedRedirects
----

. `$expectedRedirects` - The xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] with redirect parameters containing the following columns:
** [subs=+quotes]`*startUrl*` - The URL from which redirection starts.
** [subs=+quotes]`*endUrl*` - The expected final URL to redirect to.
** [subs=+quotes]`*redirectsNumber*` - The expected number of redirects between `startUrl` and `endUrl` (optional).

.Validate redirects
[source,gherkin]
----
Then I validate HTTP redirects:
|startUrl |endUrl |redirectsNumber |
|http://example.com/redirect |http://example.com/get-response |1 |
----


== HTTP cookie steps

Expand Down
25 changes: 4 additions & 21 deletions docs/modules/plugins/pages/plugin-web-app-to-rest-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -502,27 +502,10 @@ Then all resources found by CSS selector `a,img` in ${source-code} are valid

=== Validate redirects

Check that all URLs from xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] redirect to proper pages with correct redirects number. Validation fails if either actual final URL or number of redirects do not match the expected values.

NOTE: The step throws the error in case if https://developer.mozilla.org/en-US/docs/Web/HTTP/Status[HTTP response status code] of checked URL out of range 200-207.

[source,gherkin]
----
Then I validate HTTP redirects: $expectedRedirects
----

. `$expectedRedirects` - The xref:ROOT:glossary.adoc#_examplestable[ExamplesTable] with redirect parameters containing the following columns:
** [subs=+quotes]`*startUrl*` - The URL from which redirection starts.
** [subs=+quotes]`*endUrl*` - The expected final URL to redirect to.
** [subs=+quotes]`*redirectsNumber*` - The expected number of redirects between `startUrl` and `endUrl` (optional).

.Validate redirects
[source,gherkin]
----
Then I validate HTTP redirects:
|startUrl |endUrl |redirectsNumber |
|http://example.com/redirect |http://example.com/get-response |1 |
----
[WARNING]
====
The step xref:plugins:plugin-rest-api.adoc#_validate_redirects[has been moved] to `vividus-plugin-rest-api`.
====

=== Validate SSL rating

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -95,6 +95,6 @@ private void attachApiMessage(String title, Header[] headers, byte[] body, Strin
dataMap.put("bodyContentType", mimeType);
dataMap.put("statusCode", statusCode);

attachmentPublisher.publishAttachment("/org/vividus/http/attachment/api-message.ftl", dataMap, title);
attachmentPublisher.publishAttachment("/org/vividus/http/steps/attachment/api-message.ftl", dataMap, title);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps;
package org.vividus.http.steps;

import org.hamcrest.Matchers;
import org.vividus.softassert.ISoftAssert;
Expand All @@ -30,7 +30,7 @@ public void assertMatchesRule(ISoftAssert softAssert, byte[] expected, byte[] ac
{
if (Matchers.equalTo(expected).matches(actual))
{
softAssert.recordPassedAssertion(ByteArrayValidationRule.ARRAYS_ARE_EQUAL);
softAssert.recordPassedAssertion(ARRAYS_ARE_EQUAL);
}
else
{
Expand Down Expand Up @@ -59,7 +59,7 @@ public void assertMatchesRule(ISoftAssert softAssert, byte[] expected, byte[] ac
}
else
{
softAssert.recordFailedAssertion(ByteArrayValidationRule.ARRAYS_ARE_EQUAL);
softAssert.recordFailedAssertion(ARRAYS_ARE_EQUAL);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.api;
package org.vividus.http.steps;

import static org.hamcrest.Matchers.greaterThan;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.api;
package org.vividus.http.steps;

import static java.util.stream.Collectors.toList;

Expand All @@ -27,11 +27,14 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.entity.mime.ByteArrayBody;
import org.apache.hc.client5.http.entity.mime.ContentBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
Expand All @@ -48,8 +51,8 @@
import org.vividus.http.HttpMethod;
import org.vividus.http.HttpRequestExecutor;
import org.vividus.http.HttpTestContext;
import org.vividus.model.RequestPartType;
import org.vividus.steps.DataWrapper;
import org.vividus.util.ResourceUtils;

public class HttpRequestSteps
{
Expand Down Expand Up @@ -273,4 +276,80 @@ public void setApiEndpoint(String apiEndpoint)
{
this.apiEndpoint = apiEndpoint;
}

public enum RequestPartType
{
STRING
{
@Override
public ContentType getDefaultContentType()
{
return ContentType.DEFAULT_TEXT;
}

@Override
public ContentBody createPart(String value, ContentType contentType)
{
return new StringBody(value, contentType);
}

@Override
public ContentBody createPart(String value, ContentType contentType, String fileName)
{
throw new IllegalArgumentException(
String.format("'fileName' parameter is not allowed for %s request part type", name()));
}
},
FILE
{
@Override
public ContentType getDefaultContentType()
{
return ContentType.DEFAULT_BINARY;
}

@Override
public ContentBody createPart(String value, ContentType contentType) throws IOException
{
String fileName = FilenameUtils.getName(value);
return createPart(value, contentType, fileName);
}

@Override
public ContentBody createPart(String value, ContentType contentType, String fileName) throws IOException
{
byte[] byteArray = ResourceUtils.loadResourceOrFileAsByteArray(value);
return new ByteArrayBody(byteArray, contentType, fileName);
}
},
BINARY
{
@Override
public ContentType getDefaultContentType()
{
return ContentType.DEFAULT_BINARY;
}

@Override
public ContentBody createPart(String value, ContentType contentType)
{
throw new IllegalArgumentException(
String.format("'fileName' parameter is required for %s request part type", name()));
}

@Override
public ContentBody createPart(String value, ContentType contentType, String fileName)
{
byte[] byteArray = value.getBytes(StandardCharsets.UTF_8);
return new ByteArrayBody(byteArray, contentType, fileName);
}
};

public abstract ContentType getDefaultContentType();

public abstract ContentBody createPart(String value, ContentType contentType) throws IOException;

public abstract ContentBody createPart(String value, ContentType contentType, String fileName)
throws IOException;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,15 +14,15 @@
* limitations under the License.
*/

package org.vividus.steps.http;
package org.vividus.http.steps;

import java.util.List;
import java.util.Map;

import org.jbehave.core.annotations.Then;
import org.jbehave.core.model.ExamplesTable;
import org.vividus.http.validation.ResourceValidation;
import org.vividus.http.validation.ResourceValidator;
import org.vividus.http.validation.model.ResourceValidation;
import org.vividus.reporter.event.AttachmentPublisher;
import org.vividus.softassert.ISoftAssert;
import org.vividus.util.UriUtils;
Expand Down Expand Up @@ -63,7 +63,8 @@ public void verifyHttpResources(ExamplesTable resources)
.sorted()
.toList();

attachmentPublisher.publishAttachment("http-resources-validation-results.ftl",
attachmentPublisher.publishAttachment(
"/org/vividus/http/steps/attachment/http-resources-validation-results.ftl",
Map.of("results", validations), "HTTP resources validation results");
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.api;
package org.vividus.http.steps;

import static org.hamcrest.Matchers.contains;

Expand All @@ -39,7 +39,6 @@
import org.vividus.http.HttpTestContext;
import org.vividus.http.client.HttpResponse;
import org.vividus.softassert.ISoftAssert;
import org.vividus.steps.ByteArrayValidationRule;
import org.vividus.steps.ComparisonRule;
import org.vividus.steps.StringComparisonRule;
import org.vividus.steps.SubSteps;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.api;
package org.vividus.http.steps;

import java.time.Duration;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.vividus.steps.api;
package org.vividus.http.steps;

import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down
Loading

0 comments on commit 681c156

Please sign in to comment.