Skip to content

Commit

Permalink
Http support for any request body
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Dec 16, 2024
1 parent 7954505 commit 2bb40c6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.fizzed.blaze.util;

public class IntMatcher {
public class IntRange {

private final Integer from;
private final Integer to;

public IntMatcher(Integer from, Integer to) {
public IntRange(Integer from, Integer to) {
this.from = from;
this.to = to;
}
Expand All @@ -22,4 +22,8 @@ public boolean matches(Integer value) {
return value >= from && value <= to;
}

static public IntRange intRange(Integer from, Integer to) {
return new IntRange(from, to);
}

}
66 changes: 50 additions & 16 deletions blaze-http/src/main/java/com/fizzed/blaze/http/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fizzed.blaze.core.Action;
import com.fizzed.blaze.core.BlazeException;
import com.fizzed.blaze.core.VerbosityMixin;
import com.fizzed.blaze.util.IntMatcher;
import com.fizzed.blaze.util.IntRange;
import com.fizzed.blaze.util.VerboseLogger;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -36,16 +36,17 @@ static public class Result extends com.fizzed.blaze.core.Result<Http,Integer,Res
private final Request.Builder requestBuilder;
private String method;
private FormBody.Builder formBuilder;
private RequestBody body;
private Path target;
private List<IntMatcher> statusCodes;
private final List<IntRange> statusCodes;

public Http(Context context) {
super(context);
this.log = new VerboseLogger(this);
this.clientBuilder = new OkHttpClient.Builder();
this.requestBuilder = new Request.Builder();
this.statusCodes = new ArrayList<>();
this.statusCodes.add(new IntMatcher(200, 299));
this.statusCodes.add(new IntRange(200, 299));
}

public VerboseLogger getVerboseLogger() {
Expand All @@ -67,14 +68,14 @@ public Http post(String url) {
public Http statusCodes(Integer ... codes) {
this.statusCodes.clear();
for (Integer code : codes) {
this.statusCodes.add(new IntMatcher(code, code));
this.statusCodes.add(new IntRange(code, code));
}
return this;
}

public Http statusCodes(IntMatcher ... intMatchers) {
public Http statusCodes(IntRange... intRanges) {
this.statusCodes.clear();
this.statusCodes.addAll(Arrays.asList(intMatchers));
this.statusCodes.addAll(Arrays.asList(intRanges));
return this;
}

Expand All @@ -83,6 +84,16 @@ public Http statusCodesAny() {
return this;
}

public Http header(String name, String value) {
this.requestBuilder.header(name, value);
return this;
}

public Http addHeader(String name, String value) {
this.requestBuilder.addHeader(name, value);
return this;
}

public Http form(String param, String value) {
if (this.formBuilder == null) {
this.formBuilder = new FormBody.Builder();
Expand All @@ -91,13 +102,18 @@ public Http form(String param, String value) {
return this;
}

public Http header(String name, String value) {
this.requestBuilder.header(name, value);
public Http body(Path file, String contentType) {
this.body = RequestBody.create(file.toFile(), MediaType.parse(contentType));
return this;
}

public Http addHeader(String name, String value) {
this.requestBuilder.addHeader(name, value);
public Http body(byte[] bytes, String contentType) {
this.body = RequestBody.create(bytes, MediaType.parse(contentType));
return this;
}

public Http body(String str, String contentType) {
this.body = RequestBody.create(str, MediaType.parse(contentType));
return this;
}

Expand All @@ -114,27 +130,45 @@ public Http target(String target) {
return this.target(Paths.get(target));
}

private void require(RequestBody requestBody) {
if (requestBody == null) {
throw new BlazeException("Request body must be set");
}
}

@Override
protected Result doRun() throws BlazeException {
if (this.method == null) {
throw new BlazeException("Method must be set by calling .get(), .post(), etc.");
}

// is there a request body?
RequestBody body = null;
RequestBody requestBody = null;
if (this.formBuilder != null) {
body = this.formBuilder.build();
requestBody = this.formBuilder.build();
} else if (this.body != null) {
requestBody = this.body;
}

switch (this.method) {
case "get":
this.requestBuilder.get();
break;
case "post":
this.requestBuilder.post(body);
this.require(requestBody);
this.requestBuilder.post(requestBody);
break;
case "put":
this.require(requestBody);
this.requestBuilder.put(requestBody);
break;
case "patch":
this.require(requestBody);
this.requestBuilder.patch(requestBody);
break;
case "delete":
this.requestBuilder.delete(body);
this.require(requestBody);
this.requestBuilder.delete(requestBody);
break;
default:
throw new BlazeException("Unknown http method: " + this.method);
Expand Down Expand Up @@ -180,8 +214,8 @@ public Response intercept(@NotNull Chain chain) throws IOException {
// was the status code what we expect?
if (!this.statusCodes.isEmpty()) {
boolean matched = false;
for (IntMatcher intMatcher : this.statusCodes) {
if (intMatcher.matches(response.code())) {
for (IntRange intRange : this.statusCodes) {
if (intRange.matches(response.code())) {
matched = true;
break;
}
Expand Down
55 changes: 43 additions & 12 deletions blaze-http/src/test/java/com/fizzed/blaze/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,34 @@ public void get() throws InterruptedException {
}

@Test
public void unexpectedStatusCode() throws InterruptedException {
this.mockWebServer.enqueue(new MockResponse()
.setResponseCode(201).setBody("ok"));

public void requireBody() throws InterruptedException {
try {
new Http(context).get(this.getMockUrl())
new Http(context).post(this.getMockUrl())
.verbose()
.statusCodes(200)
.run();
} catch (BlazeException e) {
assertThat(e.getMessage(), containsString("code 201"));
assertThat(e.getMessage(), containsString("body"));
}
}

@Test
public void statusCodesAny() throws InterruptedException {
public void postBody() throws InterruptedException {
this.mockWebServer.enqueue(new MockResponse()
.setResponseCode(499).setBody("ok"));
.setResponseCode(201)
.setBody("ok"));

Integer code = new Http(context).get(this.getMockUrl())
int code = new Http(context).post(this.getMockUrl())
.verbose()
.statusCodesAny()
.body("body", "text/plain")
.run();

assertThat(code, is(499));
assertThat(code, is(201));

RecordedRequest rr = this.mockWebServer.takeRequest();

assertThat(rr.getMethod(), is("POST"));
assertThat(rr.getHeader("Content-Type"), is("text/plain; charset=utf-8"));
assertThat(rr.getBody().readUtf8(), is("body"));
}

@Test
Expand All @@ -98,4 +101,32 @@ public void postForm() throws InterruptedException {
assertThat(rr.getBody().readUtf8(), is("a=b"));
}

@Test
public void unexpectedStatusCode() throws InterruptedException {
this.mockWebServer.enqueue(new MockResponse()
.setResponseCode(201).setBody("ok"));

try {
new Http(context).get(this.getMockUrl())
.verbose()
.statusCodes(200)
.run();
} catch (BlazeException e) {
assertThat(e.getMessage(), containsString("code 201"));
}
}

@Test
public void statusCodesAny() throws InterruptedException {
this.mockWebServer.enqueue(new MockResponse()
.setResponseCode(499).setBody("ok"));

Integer code = new Http(context).get(this.getMockUrl())
.verbose()
.statusCodesAny()
.run();

assertThat(code, is(499));
}

}

0 comments on commit 2bb40c6

Please sign in to comment.