Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bodyMediaType to malformed request tests #2309

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
public final class HttpMalformedRequestDefinition implements ToNode, ToSmithyBuilder<HttpMalformedRequestDefinition> {

private static final String BODY = "body";
private static final String BODY_MEDIA_TYPE = "bodyMediaType";
private static final String HEADERS = "headers";
private static final String HOST = "host";
private static final String METHOD = "method";
private static final String QUERY_PARAMS = "queryParams";
private static final String URI = "uri";

private final String body;
private final String bodyMediaType;
private final Map<String, String> headers;
private final String host;
private final String method;
Expand All @@ -53,6 +55,7 @@ public final class HttpMalformedRequestDefinition implements ToNode, ToSmithyBui

private HttpMalformedRequestDefinition(Builder builder) {
body = builder.body;
bodyMediaType = builder.bodyMediaType;
host = builder.host;
headers = MapUtils.copyOf(builder.headers);
method = SmithyBuilder.requiredState(METHOD, builder.method);
Expand All @@ -64,6 +67,10 @@ public Optional<String> getBody() {
return Optional.ofNullable(body);
}

public Optional<String> getBodyMediaType() {
return Optional.ofNullable(bodyMediaType);
}

public Map<String, String> getHeaders() {
return headers;
}
Expand Down Expand Up @@ -93,6 +100,7 @@ public static HttpMalformedRequestDefinition fromNode(Node node) {
HttpMalformedRequestDefinition.Builder builder = builder();
ObjectNode o = node.expectObjectNode();
o.getStringMember(BODY).map(StringNode::getValue).ifPresent(builder::body);
o.getStringMember(BODY_MEDIA_TYPE).map(StringNode::getValue).ifPresent(builder::bodyMediaType);
o.getObjectMember(HEADERS).ifPresent(headers -> {
headers.getStringMap().forEach((k, v) -> {
builder.putHeader(k, v.expectStringNode().getValue());
Expand All @@ -111,6 +119,7 @@ public static HttpMalformedRequestDefinition fromNode(Node node) {
public Node toNode() {
return Node.objectNodeBuilder()
.withOptionalMember(BODY, getBody().map(Node::from))
.withOptionalMember(BODY_MEDIA_TYPE, getBodyMediaType().map(Node::from))
.withOptionalMember(HEADERS,
headers.isEmpty() ? Optional.empty() : Optional.of(ObjectNode.fromStringMap(getHeaders())))
.withOptionalMember(HOST, getHost().map(StringNode::from))
Expand All @@ -128,6 +137,7 @@ public Builder toBuilder() {
.method(getMethod())
.queryParams(getQueryParams());
getBody().ifPresent(builder::body);
getBodyMediaType().ifPresent(builder::bodyMediaType);
getHost().ifPresent(builder::host);
getUri().ifPresent(builder::uri);
return builder;
Expand All @@ -143,6 +153,7 @@ public static Builder builder() {
public static final class Builder implements SmithyBuilder<HttpMalformedRequestDefinition> {

private String body;
private String bodyMediaType;
private String host;
private final Map<String, String> headers = new HashMap<>();
private String method;
Expand All @@ -156,6 +167,11 @@ public Builder body(String body) {
return this;
}

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

public Builder headers(Map<String, String> headers) {
this.headers.clear();
this.headers.putAll(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ structure HttpMalformedRequestDefinition {

/// The HTTP message body to include in the request
body: String,

/// The media type of the `body`.
///
/// This is used to help test runners to parse and validate the expected
/// data against generated data.
bodyMediaType: String
}

@private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ structure testProtocol {}
"bar" : ["d", "e", "f"]
}
}
{
id: "noResponseBodyAssertionWithMediaType"
documentation: "Testing..."
protocol: testProtocol
request: {
body: "Zm9vCg=="
bodyMediaType: "application/cbor"
headers: {"X-Foo": "baz"}
host: "example.com"
method: "POST"
uri: "/"
queryParams: ["foo=baz"]
},
response: {
code: 400
headers: {"X-Foo": "baz"}
},
tags: ["foo", "bar"]
},
])
operation SayHello {
input: SayHelloInput,
Expand Down
Loading