Skip to content

Commit cf8529d

Browse files
backport #2770 patch with empty body fix to 6.X (#2779)
* backport #2770 to 6.X * fixed tests --------- Co-authored-by: Tobias Polley <mail@tobias-polley.de>
1 parent e4126c0 commit cf8529d

5 files changed

Lines changed: 28 additions & 37 deletions

File tree

core/src/main/java/com/predic8/membrane/core/http/Request.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static com.predic8.membrane.core.http.Header.*;
2929
import static com.predic8.membrane.core.http.MimeType.*;
3030
import static java.nio.charset.StandardCharsets.*;
31-
3231
public class Request extends Message {
3332

3433
private static final Pattern pattern = Pattern.compile("(.+?) (.+?) HTTP/(.+?)$");
@@ -46,17 +45,6 @@ public class Request extends Message {
4645
public static final String METHOD_OPTIONS = "OPTIONS";
4746

4847
private static final HashSet<String> methodsWithoutBody = Sets.newHashSet(METHOD_GET, METHOD_HEAD, METHOD_CONNECT);
49-
private static final HashSet<String> methodsWithOptionalBody = Sets.newHashSet(
50-
METHOD_DELETE,
51-
/* some WebDAV methods, see http://www.ietf.org/rfc/rfc2518.txt */
52-
METHOD_OPTIONS,
53-
"PROPFIND",
54-
"MKCOL",
55-
"COPY",
56-
"MOVE",
57-
"LOCK",
58-
"UNLOCK");
59-
6048

6149
String method;
6250
String uri;
@@ -154,15 +142,13 @@ public String getName() {
154142

155143
@Override
156144
public boolean shouldNotContainBody() {
157-
if (methodsWithoutBody.contains(method))
145+
if (methodsWithoutBody.contains(method)) // GET, HEAD, CONNECT
158146
return true;
159-
if (methodsWithOptionalBody.contains(method)) {
160-
if (header.hasContentLength())
161-
return header.getContentLength() == 0;
162-
return header.getFirstValue(TRANSFER_ENCODING) == null;
163-
}
164-
165-
return false;
147+
if (isHTTP10())
148+
return false;
149+
if (header.hasContentLength())
150+
return header.getContentLength() == 0;
151+
return header.getFirstValue(TRANSFER_ENCODING) == null;
166152
}
167153

168154
/**

core/src/test/java/com/predic8/membrane/core/exchangestore/LimitedMemoryExchangeStoreTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import java.io.IOException;
26+
import java.net.URISyntaxException;
2627

2728
import static org.junit.jupiter.api.Assertions.assertEquals;
2829

@@ -62,12 +63,9 @@ private void assertStore(int pos, String value) {
6263
assertEquals(value, store.getAllExchangesAsList().get(pos).getProperty("id"));
6364
}
6465

65-
private Exchange getExchange(String id) throws IOException {
66-
Exchange exc = new Exchange(null);
66+
private Exchange getExchange(String id) throws IOException, URISyntaxException {
67+
Exchange exc = Request.get("http://test/").buildExchange();
6768
exc.setProperty("id", id);
68-
Request req = new Request();
69-
req.create("GET", "http://test", "HTTP/", new Header(), null);
70-
exc.setRequest(req);
7169
exc.setResponse(Response.ok().body("<xml />").build());
7270
return exc;
7371
}

core/src/test/java/com/predic8/membrane/core/http/RequestTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.*;
2222

2323
import static com.predic8.membrane.core.Constants.*;
24+
import static com.predic8.membrane.core.http.Header.CONTENT_LENGTH;
2425
import static com.predic8.membrane.core.http.MimeType.TEXT_XML;
2526
import static com.predic8.membrane.core.http.Request.*;
2627
import static com.predic8.membrane.core.util.StringTestUtil.*;
@@ -145,9 +146,11 @@ void isNotEmpty() throws IOException, URISyntaxException {
145146
}
146147

147148
@Test
148-
void createFromStream() throws IOException {
149-
Request req = new Request();
150-
req.create("POST", "http://test", "HTTP/", new Header(), getResourceAsStream(this,"/getBank.xml"));
149+
void createFromStream() throws IOException, URISyntaxException {
150+
Request req = Request.post("http://test/")
151+
.header(CONTENT_LENGTH, "1")
152+
.body(getResourceAsStream(this,"/getBank.xml"))
153+
.build();
151154
assertFalse(req.isBodyEmpty());
152155
}
153156

core/src/test/java/com/predic8/membrane/core/interceptor/MessageAnalyserTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import org.junit.jupiter.api.*;
2020

2121
import java.io.*;
22+
import java.net.URISyntaxException;
2223

24+
import static com.predic8.membrane.core.http.Header.CONTENT_TYPE;
25+
import static com.predic8.membrane.core.http.MimeType.APPLICATION_XML;
2326
import static org.junit.jupiter.api.Assertions.*;
2427

2528
public class MessageAnalyserTest {
@@ -66,12 +69,11 @@ private Exchange getResponse(String path) throws IOException {
6669
return exc;
6770
}
6871

69-
private Exchange getRequest(String path) throws IOException {
70-
Exchange exc = new Exchange(null);
71-
Request req = new Request();
72-
req.create("POST", "http://test", "HTTP/", new Header(), getClass().getClassLoader().getResourceAsStream(path));
73-
exc.setRequest(req);
74-
return exc;
72+
private Exchange getRequest(String path) throws IOException, URISyntaxException {
73+
return Request.post("http://test/")
74+
.header(CONTENT_TYPE, APPLICATION_XML)
75+
.body(getClass().getClassLoader().getResourceAsStream(path))
76+
.buildExchange();
7577
}
7678

7779
}

core/src/test/java/com/predic8/membrane/core/interceptor/soap/SoapOperationExtractorTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.predic8.membrane.core.interceptor.soap;
1515

1616
import java.io.IOException;
17+
import java.net.URISyntaxException;
1718

1819
import org.junit.jupiter.api.BeforeAll;
1920
import org.junit.jupiter.api.Test;
@@ -72,10 +73,11 @@ public void nonEmptyHeader() throws Exception {
7273

7374
}
7475

75-
private Exchange getExchange(String path) throws IOException {
76+
private Exchange getExchange(String path) throws IOException, URISyntaxException {
7677
Exchange exc = new Exchange(null);
77-
Request req = new Request();
78-
req.create("POST", "http://test", "HTTP/", new Header(), getClass().getClassLoader().getResourceAsStream(path));
78+
Request req = Request.post("http://test/")
79+
.body(getClass().getClassLoader().getResourceAsStream(path))
80+
.build();
7981
exc.setRequest(req);
8082
return exc;
8183
}

0 commit comments

Comments
 (0)