Skip to content

Commit

Permalink
changed cors max age to duration and time unit
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamhead committed Aug 31, 2023
1 parent 27a8438 commit 39677c0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.github.dreamhead.moco.handler.cors.CorsOriginConfig;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

public final class MocoCors {
public static ResponseHandler cors(final CorsConfig... configs) {
Expand Down Expand Up @@ -57,8 +58,8 @@ public static CorsConfig exposeHeaders(final String... headers) {
return new CorsExposedHeadersConfig(headers);
}

public static CorsConfig maxAge(final long maxAge) {
return new CorsMaxAgeConfig(maxAge);
public static CorsConfig maxAge(final long maxAge, final TimeUnit unit) {
return new CorsMaxAgeConfig(unit.toSeconds(maxAge));
}

private MocoCors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static com.github.dreamhead.moco.MocoCors.allowCredentials;
import static com.github.dreamhead.moco.MocoCors.allowHeaders;
import static com.github.dreamhead.moco.MocoCors.allowMethods;
Expand Down Expand Up @@ -184,7 +186,7 @@ public void should_get_preflight_response() throws Exception {

@Test
public void should_support_cors_with_max_age() throws Exception {
server.response(cors(allowOrigin("https://www.github.com/"), maxAge(1728000)));
server.response(cors(allowOrigin("https://www.github.com/"), maxAge(1728000, TimeUnit.SECONDS)));

running(server, () -> {
ClassicHttpResponse response = helper.putForResponseWithHeaders(root(), "", of("Origin", "https://www.github.com/"));
Expand Down
4 changes: 2 additions & 2 deletions moco-doc/ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Release 1.5.0
## APIs
* `req.client.port` is supported in template
* `sameSite` cookie attribute is supported in template
* `cors` in response is supported for CORS.
* `sameSite` cookie attribute is supported
* `cors` in response is supported for CORS
* `allowOrigin`(Access-Control-Allow-Credentials)
* `allowCredentials`(Access-Control-Allow-Credentials)
* `exposeHeaders`(Access-Control-Expose-Headers)
Expand Down
12 changes: 9 additions & 3 deletions moco-doc/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ You can allow CORS with specific max age with `cors` operator with `maxAge` argu

* Java API
```java
server.response(cors(maxAge(1)));
server.response(cors(maxAge(1728000, TimeUnit.SECONDS)));
```

* JSON
Expand All @@ -1915,7 +1915,10 @@ server.response(cors(maxAge(1)));
{
"cors" :
{
"maxAge" : 1
"maxAge": {
"duration": 1728000,
"unit": "second"
}
}
}
}
Expand All @@ -1928,7 +1931,10 @@ In JSON API, you can also use `Access-Control-Max-Age` directly.
{
"cors" :
{
"Access-Control-Max-Age" : 1
"Access-Control-Max-Age" : {
"duration": 1728000,
"unit": "second"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.dreamhead.moco.parser.model.CorsContainer;
import com.github.dreamhead.moco.parser.model.LatencyContainer;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -40,7 +41,7 @@ static class InternalCorsContainer {
private List<String> allowHeaders;

@JsonAlias("Access-Control-Max-Age")
private Long maxAge;
private LatencyContainer maxAge;

@JsonAlias("Access-Control-Expose-Headers")
private List<String> exposeHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class CorsContainer {
private List<String> allowHeaders;

@JsonAlias("Access-Control-Max-Age")
private Long maxAge;
private LatencyContainer maxAge;

@JsonAlias("Access-Control-Expose-Headers")
private List<String> exposeHeaders;
Expand All @@ -46,7 +46,7 @@ public static CorsContainer newContainer() {
public static CorsContainer newContainer(final String allowOrigin,
final List<String> allowMethods,
final List<String> allowHeaders,
final long maxAge,
final LatencyContainer maxAge,
final List<String> exposeHeaders,
final Boolean allowCredentials) {
CorsContainer container = new CorsContainer();
Expand Down Expand Up @@ -74,7 +74,7 @@ public CorsConfig[] getConfigs() {
}

if (maxAge != null) {
configs.add(maxAge(maxAge));
configs.add(maxAge(maxAge.getLatency(), maxAge.getUnit()));
}

if (exposeHeaders != null) {
Expand Down
10 changes: 8 additions & 2 deletions moco-runner/src/test/resources/cors.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
"allowHeaders": [
"Content-Type, Authorization, Accept, X-Requested-With"
],
"maxAge": 1728000,
"maxAge": {
"duration": 1728000,
"unit": "second"
},
"exposeHeaders": [
"X-My-Custom-Header, X-Another-Custom-Header"
],
Expand All @@ -49,7 +52,10 @@
"Access-Control-Allow-Headers": [
"Content-Type, Authorization, Accept, X-Requested-With"
],
"Access-Control-Max-Age": 1728000,
"Access-Control-Max-Age": {
"duration": 1728000,
"unit": "second"
},
"Access-Control-Expose-Headers": [
"X-My-Custom-Header, X-Another-Custom-Header"
],
Expand Down

0 comments on commit 39677c0

Please sign in to comment.