Skip to content

Commit 19fad13

Browse files
committed
Fix the missing Location (fixes #5)
1 parent fa1fc27 commit 19fad13

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/main/java/jodd/http/HttpRequest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,11 @@ public HttpResponse send() {
885885

886886
if (HttpStatus.isRedirect(statusCode)) {
887887
_reset();
888-
set(httpResponse.location());
888+
final String location = httpResponse.location();
889+
if (location == null) {
890+
return httpResponse;
891+
}
892+
set(location);
889893
continue;
890894
}
891895

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package jodd.http;
2+
3+
import jodd.http.fixture.StringHttpRequest;
4+
import org.junit.jupiter.api.Test;
5+
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
class NoLocationTest {
10+
11+
@Test
12+
void testNoLocationInResponse() {
13+
HttpRequest httpRequest = StringHttpRequest.create(
14+
"HTTP/1.1 302 OK\n" +
15+
"Connection: close\n" +
16+
"\n");
17+
HttpResponse response = httpRequest.followRedirects(true).send();
18+
assertEquals(302, response.statusCode());
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package jodd.http.fixture;
2+
3+
import jodd.http.HttpConnection;
4+
import jodd.http.HttpConnectionProvider;
5+
import jodd.http.HttpRequest;
6+
import jodd.http.ProxyInfo;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.InputStream;
11+
import java.io.OutputStream;
12+
13+
public class StringHttpRequest {
14+
15+
public static HttpRequest create(final String payload) {
16+
return new HttpRequest().withConnectionProvider(new HttpConnectionProvider() {
17+
@Override
18+
public void useProxy(ProxyInfo proxyInfo) {
19+
}
20+
21+
@Override
22+
public HttpConnection createHttpConnection(HttpRequest httpRequest) {
23+
return new HttpConnection() {
24+
25+
private final ByteArrayInputStream in = new ByteArrayInputStream(payload.getBytes());
26+
private final ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
27+
28+
@Override
29+
public void init() {
30+
}
31+
32+
@Override
33+
public OutputStream getOutputStream() {
34+
return out;
35+
}
36+
37+
@Override
38+
public InputStream getInputStream() {
39+
return in;
40+
}
41+
42+
@Override
43+
public void close() {
44+
}
45+
46+
@Override
47+
public void setTimeout(int milliseconds) {
48+
}
49+
};
50+
}
51+
});
52+
}
53+
}

0 commit comments

Comments
 (0)