|
32 | 32 | import org.springframework.http.ResponseEntity; |
33 | 33 | import org.springframework.util.LinkedMultiValueMap; |
34 | 34 | import org.springframework.util.MultiValueMap; |
| 35 | +import org.springframework.web.client.HttpServerErrorException; |
35 | 36 | import org.springframework.web.client.RestOperations; |
36 | 37 | import org.springframework.web.client.RestTemplate; |
37 | 38 |
|
@@ -62,28 +63,84 @@ public static void run() { |
62 | 63 | result = helloWorld.sayHello(); |
63 | 64 | TestMgr.check("hello world", result); |
64 | 65 |
|
65 | | - testCorsHandler(); |
| 66 | + testCorsHandlerOptions(); |
| 67 | + testCorsHandlerGet(); |
66 | 68 |
|
67 | 69 | TestMgr.summary(); |
68 | 70 | System.setProperty("sun.net.http.allowRestrictedHeaders", "false"); |
69 | 71 | } |
70 | 72 |
|
71 | | - private static void testCorsHandler() { |
| 73 | + private static void testCorsHandlerOptions() { |
| 74 | + // first domain |
72 | 75 | RestOperations springRestTemplate = new RestTemplate(); |
73 | 76 | MultiValueMap<String, String> requestHeaders = new LinkedMultiValueMap<>(); |
74 | | - requestHeaders.put("Origin", Collections.singletonList("http://localhost:8080")); |
| 77 | + requestHeaders.put("Origin", Collections.singletonList("http://test.domain:8080")); |
75 | 78 | requestHeaders.put("Access-Control-Request-Method", Collections.singletonList("PUT")); |
76 | | - |
77 | 79 | HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeaders); |
78 | 80 | ResponseEntity<String> responseEntity = springRestTemplate |
79 | 81 | .exchange("http://127.0.0.1:8080/helloworld/hello", HttpMethod.OPTIONS, requestEntity, |
80 | 82 | String.class); |
81 | | - |
82 | 83 | TestMgr.check("204", responseEntity.getStatusCode().value()); |
83 | 84 | TreeSet<String> sortedSet = new TreeSet<>(responseEntity.getHeaders().get("Access-Control-Allow-Methods")); |
84 | 85 | TestMgr.check("[DELETE,POST,GET,PUT]", sortedSet); |
85 | 86 | sortedSet = new TreeSet<>(responseEntity.getHeaders().get("Access-Control-Allow-Headers")); |
86 | 87 | TestMgr.check("[abc,def]", sortedSet); |
87 | | - TestMgr.check("*", responseEntity.getHeaders().getFirst("Access-Control-Allow-Origin")); |
| 88 | + TestMgr.check("http://test.domain:8080", |
| 89 | + responseEntity.getHeaders().getFirst("Access-Control-Allow-Origin")); |
| 90 | + |
| 91 | + // second domain |
| 92 | + requestHeaders = new LinkedMultiValueMap<>(); |
| 93 | + requestHeaders.put("Origin", Collections.singletonList("http://test.domain:9090")); |
| 94 | + requestHeaders.put("Access-Control-Request-Method", Collections.singletonList("PUT")); |
| 95 | + requestEntity = new HttpEntity<>(requestHeaders); |
| 96 | + responseEntity = springRestTemplate |
| 97 | + .exchange("http://127.0.0.1:8080/helloworld/hello", HttpMethod.OPTIONS, requestEntity, |
| 98 | + String.class); |
| 99 | + TestMgr.check("204", responseEntity.getStatusCode().value()); |
| 100 | + sortedSet = new TreeSet<>(responseEntity.getHeaders().get("Access-Control-Allow-Methods")); |
| 101 | + TestMgr.check("[DELETE,POST,GET,PUT]", sortedSet); |
| 102 | + sortedSet = new TreeSet<>(responseEntity.getHeaders().get("Access-Control-Allow-Headers")); |
| 103 | + TestMgr.check("[abc,def]", sortedSet); |
| 104 | + TestMgr.check("http://test.domain:9090", |
| 105 | + responseEntity.getHeaders().getFirst("Access-Control-Allow-Origin")); |
| 106 | + } |
| 107 | + |
| 108 | + private static void testCorsHandlerGet() { |
| 109 | + // allowed origin |
| 110 | + RestOperations springRestTemplate = new RestTemplate(); |
| 111 | + MultiValueMap<String, String> requestHeaders = new LinkedMultiValueMap<>(); |
| 112 | + requestHeaders.put("Origin", Collections.singletonList("http://test.domain:8080")); |
| 113 | + HttpEntity<Object> requestEntity = new HttpEntity<>(requestHeaders); |
| 114 | + ResponseEntity<String> responseEntity = springRestTemplate |
| 115 | + .exchange("http://127.0.0.1:8080/helloworld/hello", HttpMethod.GET, requestEntity, |
| 116 | + String.class); |
| 117 | + |
| 118 | + TestMgr.check("200", responseEntity.getStatusCode().value()); |
| 119 | + TestMgr.check("hello world", responseEntity.getBody()); |
| 120 | + |
| 121 | + // allowed origin |
| 122 | + requestHeaders = new LinkedMultiValueMap<>(); |
| 123 | + requestHeaders.put("Origin", Collections.singletonList("http://test.domain:9090")); |
| 124 | + requestEntity = new HttpEntity<>(requestHeaders); |
| 125 | + responseEntity = springRestTemplate |
| 126 | + .exchange("http://127.0.0.1:8080/helloworld/hello", HttpMethod.GET, requestEntity, |
| 127 | + String.class); |
| 128 | + |
| 129 | + TestMgr.check("200", responseEntity.getStatusCode().value()); |
| 130 | + TestMgr.check("hello world", responseEntity.getBody()); |
| 131 | + |
| 132 | + // not allowed origin |
| 133 | + try { |
| 134 | + requestHeaders = new LinkedMultiValueMap<>(); |
| 135 | + requestHeaders.put("Origin", Collections.singletonList("http://test.domain:7070")); |
| 136 | + requestEntity = new HttpEntity<>(requestHeaders); |
| 137 | + springRestTemplate |
| 138 | + .exchange("http://127.0.0.1:8080/helloworld/hello", HttpMethod.GET, requestEntity, |
| 139 | + String.class); |
| 140 | + TestMgr.fail("must throw"); |
| 141 | + } catch (HttpServerErrorException e) { |
| 142 | + TestMgr.check(500, e.getStatusCode().value()); |
| 143 | + TestMgr.check(true, e.getMessage().contains("500 CORS Rejected")); |
| 144 | + } |
88 | 145 | } |
89 | 146 | } |
0 commit comments