Skip to content

Commit

Permalink
Bugfix: response header not reflecting correct version of http #1689 (#…
Browse files Browse the repository at this point in the history
…1890)

* change to be reverted

* remove http protocol hardcoded value in Response.java

* revert readme change

* format the files

* handle review comments

Co-authored-by: poojitha <poojitha.dubba@thoughtworks.com>
  • Loading branch information
SaiPoojithaDubba and poojitha committed Jun 6, 2022
1 parent 6728eb7 commit 90145f9
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Thomas Akehurst
* Copyright (C) 2018-2022 Thomas Akehurst
*
* 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 @@ -279,6 +279,11 @@ public Optional<Request> getOriginalRequest() {
return delegate.getOriginalRequest();
}

@Override
public String getProtocol() {
return delegate.getProtocol();
}

public static class Builder {

private RequestMethod requestMethod;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* 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 @@ -72,6 +72,7 @@ public void handle(Request request, HttpResponder httpResponder) {
ResponseDefinition responseDefinition = serveEvent.getResponseDefinition();
responseDefinition.setOriginalRequest(processedRequest);
Response response = responseRenderer.render(serveEvent);
response = Response.Builder.like(response).protocol(request.getProtocol()).build();
ServeEvent completedServeEvent =
serveEvent.complete(response, (int) stopwatch.elapsed(MILLISECONDS));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* 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 @@ -77,4 +77,6 @@ interface Part {
boolean isBrowserProxyRequest();

Optional<Request> getOriginalRequest();

String getProtocol();
}
41 changes: 27 additions & 14 deletions src/main/java/com/github/tomakehurst/wiremock/http/Response.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* 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 @@ -39,10 +39,11 @@ public class Response {
private final boolean fromProxy;
private final long initialDelay;
private final ChunkedDribbleDelay chunkedDribbleDelay;
private final String protocol;

public static Response notConfigured() {
return new Response(
HTTP_NOT_FOUND, null, (byte[]) null, noHeaders(), false, null, 0, null, false);
HTTP_NOT_FOUND, null, (byte[]) null, noHeaders(), false, null, 0, null, false, null);
}

public static Builder response() {
Expand All @@ -58,7 +59,8 @@ public Response(
Fault fault,
long initialDelay,
ChunkedDribbleDelay chunkedDribbleDelay,
boolean fromProxy) {
boolean fromProxy,
String protocol) {
this.status = status;
this.statusMessage = statusMessage;
this.bodyStreamSource = StreamSources.forBytes(body);
Expand All @@ -68,6 +70,7 @@ public Response(
this.initialDelay = initialDelay;
this.chunkedDribbleDelay = chunkedDribbleDelay;
this.fromProxy = fromProxy;
this.protocol = protocol;
}

public Response(
Expand All @@ -79,7 +82,8 @@ public Response(
Fault fault,
long initialDelay,
ChunkedDribbleDelay chunkedDribbleDelay,
boolean fromProxy) {
boolean fromProxy,
String protocol) {
this.status = status;
this.statusMessage = statusMessage;
this.bodyStreamSource = streamSource;
Expand All @@ -89,6 +93,7 @@ public Response(
this.initialDelay = initialDelay;
this.chunkedDribbleDelay = chunkedDribbleDelay;
this.fromProxy = fromProxy;
this.protocol = protocol;
}

public Response(
Expand All @@ -100,7 +105,8 @@ public Response(
Fault fault,
long initialDelay,
ChunkedDribbleDelay chunkedDribbleDelay,
boolean fromProxy) {
boolean fromProxy,
String protocol) {
this.status = status;
this.statusMessage = statusMessage;
this.headers = headers;
Expand All @@ -110,6 +116,7 @@ public Response(
this.initialDelay = initialDelay;
this.chunkedDribbleDelay = chunkedDribbleDelay;
this.fromProxy = fromProxy;
this.protocol = protocol;
}

public int getStatus() {
Expand Down Expand Up @@ -170,11 +177,7 @@ public boolean isFromProxy() {

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("HTTP/1.1 ").append(status).append("\n");
sb.append(headers).append("\n");
// no longer printing body
return sb.toString();
return protocol + " " + status + "\n" + headers;
}

public static class Builder {
Expand All @@ -189,6 +192,7 @@ public static class Builder {
private boolean fromProxy;
private long initialDelay;
private ChunkedDribbleDelay chunkedDribbleDelay;
private String protocol;

public static Builder like(Response response) {
Builder responseBuilder = new Builder();
Expand Down Expand Up @@ -320,7 +324,8 @@ public Response build() {
fault,
initialDelay,
chunkedDribbleDelay,
fromProxy);
fromProxy,
protocol);
} else if (bodyString != null) {
return new Response(
status,
Expand All @@ -331,7 +336,8 @@ public Response build() {
fault,
initialDelay,
chunkedDribbleDelay,
fromProxy);
fromProxy,
protocol);
} else if (bodyStream != null) {
return new Response(
status,
Expand All @@ -342,7 +348,8 @@ public Response build() {
fault,
initialDelay,
chunkedDribbleDelay,
fromProxy);
fromProxy,
protocol);
} else {
return new Response(
status,
Expand All @@ -353,8 +360,14 @@ public Response build() {
fault,
initialDelay,
chunkedDribbleDelay,
fromProxy);
fromProxy,
protocol);
}
}

public Builder protocol(final String protocol) {
this.protocol = protocol;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,9 @@ public Optional<Request> getOriginalRequest() {
public String toString() {
return request.toString() + getBodyAsString();
}

@Override
public String getProtocol() {
return request.getProtocol();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* 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 @@ -56,6 +56,7 @@ public class LoggedRequest implements Request {
private final boolean isBrowserProxyRequest;
private final Date loggedDate;
private final Collection<Part> multiparts;
private final String protocol;

public static LoggedRequest createFrom(Request request) {
return new LoggedRequest(
Expand All @@ -68,7 +69,8 @@ public static LoggedRequest createFrom(Request request) {
request.isBrowserProxyRequest(),
new Date(),
request.getBody(),
request.getParts());
request.getParts(),
request.getProtocol());
}

@JsonCreator
Expand All @@ -83,7 +85,8 @@ public LoggedRequest(
@JsonProperty("loggedDate") Date loggedDate,
@JsonProperty("bodyAsBase64") String bodyAsBase64,
@JsonProperty("body") String ignoredBodyOnlyUsedForBinding,
@JsonProperty("multiparts") Collection<Part> multiparts) {
@JsonProperty("multiparts") Collection<Part> multiparts,
@JsonProperty("protocol") String protocol) {
this(
url,
absoluteUrl,
Expand All @@ -94,7 +97,8 @@ public LoggedRequest(
isBrowserProxyRequest,
loggedDate,
decodeBase64(bodyAsBase64),
multiparts);
multiparts,
protocol);
}

public LoggedRequest(
Expand All @@ -107,7 +111,8 @@ public LoggedRequest(
boolean isBrowserProxyRequest,
Date loggedDate,
byte[] body,
Collection<Part> multiparts) {
Collection<Part> multiparts,
String protocol) {
this.url = url;

this.absoluteUrl = absoluteUrl;
Expand All @@ -131,6 +136,7 @@ public LoggedRequest(
this.isBrowserProxyRequest = isBrowserProxyRequest;
this.loggedDate = loggedDate;
this.multiparts = multiparts;
this.protocol = protocol;
}

@Override
Expand Down Expand Up @@ -258,6 +264,11 @@ public Optional<Request> getOriginalRequest() {
return Optional.absent();
}

@Override
public String getProtocol() {
return protocol;
}

public Date getLoggedDate() {
return loggedDate;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 Thomas Akehurst
* Copyright (C) 2018-2022 Thomas Akehurst
*
* 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 @@ -144,6 +144,11 @@ public Optional<Request> getOriginalRequest() {
return target.getOriginalRequest();
}

@Override
public String getProtocol() {
return target.getProtocol();
}

@Override
public String toString() {
return " ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ private ServeEvent serveEvent(String path, boolean isBrowserProxyRequest, byte[]
/* isBrowserProxyRequest = */ isBrowserProxyRequest,
/* loggedDate = */ new Date(),
/* body = */ body,
/* multiparts = */ null);
/* multiparts = */ null,
/* protocol = */ "HTTP/1.1");
ResponseDefinition responseDefinition = aResponse().proxiedFrom(origin.baseUrl()).build();
responseDefinition.setOriginalRequest(loggedRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class MockRequest implements Request {
private String clientIp = "1.1.1.1";
private Collection<Part> multiparts = null;
private boolean isBrowserProxyRequest = false;
private String protocol = "HTTP/1.1";

public static MockRequest mockRequest() {
return new MockRequest();
Expand Down Expand Up @@ -122,6 +123,11 @@ public MockRequest isBrowserProxyRequest(boolean isBrowserProxyRequest) {
return this;
}

public MockRequest protocol(String protocol) {
this.protocol = protocol;
return this;
}

@Override
public String getUrl() {
return url;
Expand Down Expand Up @@ -230,6 +236,11 @@ public Optional<Request> getOriginalRequest() {
return Optional.absent();
}

@Override
public String getProtocol() {
return protocol;
}

public LoggedRequest asLoggedRequest() {
return LoggedRequest.createFrom(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2022 Thomas Akehurst
*
* 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 @@ -75,17 +75,30 @@ public void returnsResponseIndicatedByMappings() {
when(stubServer.serveStubFor(any(Request.class)))
.thenReturn(
ServeEvent.of(
mockRequest().asLoggedRequest(), new ResponseDefinition(200, "Body content")));

Response mockResponse = response().status(200).body("Body content").build();
mockRequest().protocol("HTTP/2").asLoggedRequest(),
new ResponseDefinition(200, "Body content")));

Response mockResponse =
response()
.status(200)
.body("Body content")
.headers(
new HttpHeaders(
new HttpHeader("Content-Type", "application/json"),
new HttpHeader("Matched-Stub-Id", "123")))
.build();
when(responseRenderer.render(any(ServeEvent.class))).thenReturn(mockResponse);

Request request = aRequest().withUrl("/the/required/resource").withMethod(GET).build();
Request request =
aRequest().withUrl("/the/required/resource").withMethod(GET).withProtocol("HTTP/2").build();
requestHandler.handle(request, httpResponder);
Response response = httpResponder.response;

assertThat(response.getStatus(), is(200));
assertThat(response.getBodyAsString(), is("Body content"));
assertThat(
response.toString(),
is("HTTP/2 200\nContent-Type: [application/json]\nMatched-Stub-Id: [123]\n"));
}

@Test
Expand Down

0 comments on commit 90145f9

Please sign in to comment.