Skip to content

Commit

Permalink
Add javadoc and adjust the API a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
filiphr committed Sep 4, 2024
1 parent 3d3f62b commit 30f8f8a
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 51 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
<!-- value comes from property git.commit.author.time -->
<project.build.outputTimestamp>${git.commit.author.time}</project.build.outputTimestamp>

Expand Down
125 changes: 125 additions & 0 deletions src/main/java/org/flowable/mockwebserver/MockHttpStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,176 @@

/**
* An enum of some known HTTP status codes.
* This is inspired by the Spring Framework HttpStatus enum.
*
* @author Filip Hrisafov
*/
public enum MockHttpStatus implements MockHttpStatusCode {

// 2xx Success

/**
* {@code 200 OK}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.1">HTTP/1.1: Semantics and Content, section 6.3.1</a>
*/
OK(200, "OK"),
/**
* {@code 201 Created}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.2">HTTP/1.1: Semantics and Content, section 6.3.2</a>
*/
CREATED(201, "Created"),
/**
* {@code 202 Accepted}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.3">HTTP/1.1: Semantics and Content, section 6.3.3</a>
*/
ACCEPTED(202, "Accepted"),
/**
* {@code 204 No Content}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.3.5">HTTP/1.1: Semantics and Content, section 6.3.5</a>
*/
NO_CONTENT(204, "No Content"),

// 3xx Redirection

/**
* {@code 300 Multiple Choices}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.1">HTTP/1.1: Semantics and Content, section 6.4.1</a>
*/
MULTIPLE_CHOICES(300, "Multiple Choices"),
/**
* {@code 301 Moved Permanently}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.2">HTTP/1.1: Semantics and Content, section 6.4.2</a>
*/
MOVED_PERMANENTLY(301, "Moved Permanently"),
/**
* {@code 302 Found}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.3">HTTP/1.1: Semantics and Content, section 6.4.3</a>
*/
FOUND(302, "Found"),
/**
* {@code 303 See Other}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.4">HTTP/1.1: Semantics and Content, section 6.4.4</a>
*/
SEE_OTHER(303, "See Other"),
/**
* {@code 304 Not Modified}.
* @see <a href="https://tools.ietf.org/html/rfc7232#section-4.1">HTTP/1.1: Conditional Requests, section 4.1</a>
*/
NOT_MODIFIED(304, "Not Modified"),
/**
* {@code 307 Temporary Redirect}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.4.7">HTTP/1.1: Semantics and Content, section 6.4.7</a>
*/
TEMPORARY_REDIRECT(307, "Temporary Redirect"),
/**
* {@code 308 Permanent Redirect}.
* @see <a href="https://tools.ietf.org/html/rfc7238">RFC 7238</a>
*/
PERMANENT_REDIRECT(308, "Permanent Redirect"),

// 4xx Client Error

/**
* {@code 400 Bad Request}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.1">HTTP/1.1: Semantics and Content, section 6.5.1</a>
*/
BAD_REQUEST(400, "Bad Request"),
/**
* {@code 401 Unauthorized}.
* @see <a href="https://tools.ietf.org/html/rfc7235#section-3.1">HTTP/1.1: Authentication, section 3.1</a>
*/
UNAUTHORIZED(401, "Unauthorized"),
/**
* {@code 402 Payment Required}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.2">HTTP/1.1: Semantics and Content, section 6.5.2</a>
*/
PAYMENT_REQUIRED(402, "Payment Required"),
/**
* {@code 403 Forbidden}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.3">HTTP/1.1: Semantics and Content, section 6.5.3</a>
*/
FORBIDDEN(403, "Forbidden"),
/**
* {@code 404 Not Found}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.4">HTTP/1.1: Semantics and Content, section 6.5.4</a>
*/
NOT_FOUND(404, "Not Found"),
/**
* {@code 405 Method Not Allowed}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.5">HTTP/1.1: Semantics and Content, section 6.5.5</a>
*/
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
/**
* {@code 406 Not Acceptable}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.6">HTTP/1.1: Semantics and Content, section 6.5.6</a>
*/
NOT_ACCEPTABLE(406, "Not Acceptable"),
/**
* {@code 409 Conflict}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.8">HTTP/1.1: Semantics and Content, section 6.5.8</a>
*/
CONFLICT(409, "Conflict"),
/**
* {@code 411 Length Required}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.10">
* HTTP/1.1: Semantics and Content, section 6.5.10</a>
*/
LENGTH_REQUIRED(411, "Length Required"),
/**
* {@code 413 Payload Too Large}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.11">
* HTTP/1.1: Semantics and Content, section 6.5.11</a>
*/
PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
/**
* {@code 414 URI Too Long}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.12">
* HTTP/1.1: Semantics and Content, section 6.5.12</a>
*/
URI_TOO_LONG(414, "URI Too Long"),
/**
* {@code 415 Unsupported Media Type}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.5.13">
* HTTP/1.1: Semantics and Content, section 6.5.13</a>
*/
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
/**
* {@code 418 I'm a teapot}.
* @see <a href="https://tools.ietf.org/html/rfc2324#section-2.3.2">HTCPCP/1.0</a>
*/
I_AM_A_TEAPOT(418, "I'm a teapot"),
/**
* {@code 429 Too Many Requests}.
* @see <a href="https://tools.ietf.org/html/rfc6585#section-4">Additional HTTP Status Codes</a>
*/
TOO_MANY_REQUESTS(429, "Too Many Requests"),

// 5xx Server Error

/**
* {@code 500 Internal Server Error}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.1">HTTP/1.1: Semantics and Content, section 6.6.1</a>
*/
INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
/**
* {@code 501 Not Implemented}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.2">HTTP/1.1: Semantics and Content, section 6.6.2</a>
*/
NOT_IMPLEMENTED(501, "Not Implemented"),
/**
* {@code 502 Bad Gateway}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.3">HTTP/1.1: Semantics and Content, section 6.6.3</a>
*/
BAD_GATEWAY(502, "Bad Gateway"),
/**
* {@code 503 Service Unavailable}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.4">HTTP/1.1: Semantics and Content, section 6.6.4</a>
*/
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
/**
* {@code 504 Gateway Timeout}.
* @see <a href="https://tools.ietf.org/html/rfc7231#section-6.6.5">HTTP/1.1: Semantics and Content, section 6.6.5</a>
*/
GATEWAY_TIMEOUT(504, "Gateway Timeout"),
;

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/flowable/mockwebserver/MockHttpStatusCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* Represents an HTTP status code.
* This is inspired by the Spring Framework HttpStatusCode interface.
*
* @author Filip Hrisafov
* @see MockHttpStatus for known status codes
Expand All @@ -22,11 +23,15 @@
public sealed interface MockHttpStatusCode permits MockHttpStatus, CustomMockHttpStatus {

/**
* The HTTP status code.
*
* @return The HTTP status code
*/
int code();

/**
* The reason for the HTTP status code.
*
* @return The reason for the HTTP status code
*/
String reason();
Expand Down Expand Up @@ -73,6 +78,13 @@ static MockHttpStatusCode from(int code) {
};
}

/**
* Create a custom {@link MockHttpStatusCode} with the given code and reason
*
* @param code the code to create the status for
* @param reason the custom reason for the code
* @return the custom status
*/
static MockHttpStatusCode from(int code, String reason) {
return new CustomMockHttpStatus(code, reason);
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/org/flowable/mockwebserver/MockResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,24 @@
import org.microhttp.Response;

/**
* The response that will be returned by the {@link MockWebServer} when a request is made.
*
* @author Filip Hrisafov
*/
public class MockResponse {
public final class MockResponse {

protected final Response response;
protected final Duration delay;
final Response response;
final Duration delay;

protected MockResponse(Response response, Duration delay) {
MockResponse(Response response, Duration delay) {
this.response = response;
this.delay = delay;
}

/**
* Create a new builder for a {@link MockResponse} with a default status of {@link MockHttpStatus#OK}
* Create a new builder for a {@link MockResponse} with a default status of {@link MockHttpStatus#OK}.
*
* @return The builder for fluent API
*/
public static MockResponseBuilder newBuilder() {
return MockResponseBuilder.newBuilder();
Expand Down
Loading

0 comments on commit 30f8f8a

Please sign in to comment.