Skip to content

Commit

Permalink
Add host validation to http request tests
Browse files Browse the repository at this point in the history
This adds the ability for the http request tests to model assertions
on the host. This can be useful for asserting that the endpoint trait
is properly supported, as well as allowing for testing things like
AWS clients' ability to resolve hosts based on the service id and
region.
  • Loading branch information
JordonPhillips committed Feb 5, 2021
1 parent 4bbe4f4 commit ec280a8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/source/1.0/spec/http-protocol-compliance-tests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ that support the following members:
- ``string``
- **Required**. The request-target of the HTTP request, not including
the query string (for example, "/foo/bar").
* - host
- ``string``
- The host / endpoint provided to the client, not including the path or
scheme (for example, "example.com").
* - resolvedHost
- ``string``
- The host / endpoint that the client should send to, not including the
path or scheme (for example, "prefix.example.com").

This can differ from the host provided to the client if, for instance,
the operation has a member with the :ref:`endpoint-trait`.
* - authScheme
- shape ID
- A shape ID that specifies the optional authentication scheme to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
Expand All @@ -32,12 +33,16 @@ public final class HttpRequestTestCase extends HttpMessageTestCase implements To

private static final String METHOD = "method";
private static final String URI = "uri";
private static final String HOST = "host";
private static final String RESOLVED_HOST = "resolvedHost";
private static final String QUERY_PARAMS = "queryParams";
private static final String FORBID_QUERY_PARAMS = "forbidQueryParams";
private static final String REQUIRE_QUERY_PARAMS = "requireQueryParams";

private final String method;
private final String uri;
private final String host;
private final String resolvedHost;
private final List<String> queryParams;
private final List<String> forbidQueryParams;
private final List<String> requireQueryParams;
Expand All @@ -46,6 +51,8 @@ private HttpRequestTestCase(Builder builder) {
super(builder);
method = SmithyBuilder.requiredState(METHOD, builder.method);
uri = SmithyBuilder.requiredState(URI, builder.uri);
host = builder.host;
resolvedHost = builder.resolvedHost;
queryParams = ListUtils.copyOf(builder.queryParams);
forbidQueryParams = ListUtils.copyOf(builder.forbidQueryParams);
requireQueryParams = ListUtils.copyOf(builder.requireQueryParams);
Expand All @@ -59,6 +66,14 @@ public String getUri() {
return uri;
}

public Optional<String> getHost() {
return Optional.ofNullable(host);
}

public Optional<String> getResolvedHost() {
return Optional.ofNullable(resolvedHost);
}

public List<String> getQueryParams() {
return queryParams;
}
Expand All @@ -77,6 +92,8 @@ public static HttpRequestTestCase fromNode(Node node) {
ObjectNode o = node.expectObjectNode();
builder.method(o.expectStringMember(METHOD).getValue());
builder.uri(o.expectStringMember(URI).getValue());
o.getStringMember(HOST).ifPresent(stringNode -> builder.host(stringNode.getValue()));
o.getStringMember(RESOLVED_HOST).ifPresent(stringNode -> builder.resolvedHost(stringNode.getValue()));
o.getArrayMember(QUERY_PARAMS).ifPresent(queryParams -> {
builder.queryParams(queryParams.getElementsAs(StringNode::getValue));
});
Expand All @@ -94,6 +111,8 @@ public Node toNode() {
ObjectNode.Builder node = super.toNode().expectObjectNode().toBuilder();
node.withMember(METHOD, getMethod());
node.withMember(URI, getUri());
node.withOptionalMember(HOST, getHost().map(StringNode::from));
node.withOptionalMember(RESOLVED_HOST, getResolvedHost().map(StringNode::from));
if (!queryParams.isEmpty()) {
node.withMember(QUERY_PARAMS, ArrayNode.fromStrings(getQueryParams()));
}
Expand All @@ -114,6 +133,8 @@ public Builder toBuilder() {
.queryParams(getQueryParams())
.forbidQueryParams(getForbidQueryParams())
.requireQueryParams(getRequireQueryParams());
getHost().ifPresent(builder::host);
getResolvedHost().ifPresent(builder::resolvedHost);
updateBuilder(builder);
return builder;
}
Expand All @@ -129,6 +150,8 @@ public static final class Builder extends HttpMessageTestCase.Builder<Builder, H

private String method;
private String uri;
private String host;
private String resolvedHost;
private final List<String> queryParams = new ArrayList<>();
private final List<String> forbidQueryParams = new ArrayList<>();
private final List<String> requireQueryParams = new ArrayList<>();
Expand All @@ -145,6 +168,16 @@ public Builder uri(String uri) {
return this;
}

public Builder host(String host) {
this.host = host;
return this;
}

public Builder resolvedHost(String resolvedHost) {
this.resolvedHost = resolvedHost;
return this;
}

public Builder queryParams(List<String> queryParams) {
this.queryParams.clear();
this.queryParams.addAll(queryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ structure HttpRequestTestCase {
@length(min: 1)
uri: String,

/// The host / endpoint provided to the client, not including the path
/// or scheme (for example, "example.com").
host: String,

/// The host / endpoint that the client should send to, not including
/// the path or scheme (for example, "prefix.example.com").
///
/// This can differ from the host provided to the client if the `hostPrefix`
/// member of the `endpoint` trait is set, for instance.
resolvedHost: String,

/// The optional authentication scheme shape ID to assume. It's
/// possible that specific authentication schemes might influence
/// the serialization logic of an HTTP request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ structure testScheme {}
authScheme: testScheme,
method: "POST",
uri: "/",
host: "example.com",
resolvedHost: "prefix.example.com",
queryParams: ["foo=baz"],
forbidQueryParams: ["Nope"],
requireQueryParams: ["Yap"],
Expand Down

0 comments on commit ec280a8

Please sign in to comment.