|
| 1 | +package com.baeldung.kong; |
| 2 | + |
| 3 | +import com.baeldung.kong.domain.APIObject; |
| 4 | +import com.baeldung.kong.domain.ConsumerObject; |
| 5 | +import com.baeldung.kong.domain.KeyAuthObject; |
| 6 | +import com.baeldung.kong.domain.PluginObject; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.context.SpringBootTest; |
| 12 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 13 | +import org.springframework.http.*; |
| 14 | +import org.springframework.test.context.junit4.SpringRunner; |
| 15 | + |
| 16 | +import java.net.URI; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author aiet |
| 24 | + */ |
| 25 | +@RunWith(SpringRunner.class) |
| 26 | +@SpringBootTest(webEnvironment = DEFINED_PORT, classes = StockApp.class) |
| 27 | +public class KongAdminAPILiveTest { |
| 28 | + |
| 29 | + private String getStockPrice(String code) { |
| 30 | + try { |
| 31 | + return restTemplate.getForObject(new URI("http://localhost:8080/stock/" + code), String.class); |
| 32 | + } catch (Exception ignored) { |
| 33 | + } |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + @Before |
| 38 | + public void init() { |
| 39 | + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); |
| 40 | + } |
| 41 | + |
| 42 | + @Autowired TestRestTemplate restTemplate; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenEndpoint_whenQueryStockPrice_thenPriceCorrect() { |
| 46 | + String response = getStockPrice("btc"); |
| 47 | + assertEquals("10000", response); |
| 48 | + |
| 49 | + response = getStockPrice("eth"); |
| 50 | + assertEquals("N/A", response); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong() throws Exception { |
| 55 | + restTemplate.delete("http://localhost:8001/apis/stock-api"); |
| 56 | + |
| 57 | + APIObject stockAPI = new APIObject("stock-api", "stock.api", "http://localhost:8080", "/"); |
| 58 | + HttpEntity<APIObject> apiEntity = new HttpEntity<>(stockAPI); |
| 59 | + ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); |
| 60 | + |
| 61 | + assertEquals(HttpStatus.CREATED, addAPIResp.getStatusCode()); |
| 62 | + |
| 63 | + addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); |
| 64 | + assertEquals(HttpStatus.CONFLICT, addAPIResp.getStatusCode()); |
| 65 | + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); |
| 66 | + |
| 67 | + assertTrue(apiListResp.contains("stock-api")); |
| 68 | + |
| 69 | + HttpHeaders headers = new HttpHeaders(); |
| 70 | + headers.set("Host", "stock.api"); |
| 71 | + RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); |
| 72 | + ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class); |
| 73 | + |
| 74 | + assertEquals("10000", stockPriceResp.getBody()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void givenKongAdminAPI_whenAddAPIConsumer_thenAdded() { |
| 79 | + restTemplate.delete("http://localhost:8001/consumers/eugenp"); |
| 80 | + |
| 81 | + ConsumerObject consumer = new ConsumerObject("eugenp"); |
| 82 | + HttpEntity<ConsumerObject> addConsumerEntity = new HttpEntity<>(consumer); |
| 83 | + ResponseEntity<String> addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers/", addConsumerEntity, String.class); |
| 84 | + |
| 85 | + assertEquals(HttpStatus.CREATED, addConsumerResp.getStatusCode()); |
| 86 | + |
| 87 | + addConsumerResp = restTemplate.postForEntity("http://localhost:8001/consumers", addConsumerEntity, String.class); |
| 88 | + assertEquals(HttpStatus.CONFLICT, addConsumerResp.getStatusCode()); |
| 89 | + |
| 90 | + String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class); |
| 91 | + assertTrue(consumerListResp.contains("eugenp")); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void givenAPI_whenEnableAuth_thenAnonymousDenied() throws Exception { |
| 96 | + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); |
| 97 | + if (!apiListResp.contains("stock-api")) { |
| 98 | + givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong(); |
| 99 | + } |
| 100 | + |
| 101 | + PluginObject authPlugin = new PluginObject("key-auth"); |
| 102 | + ResponseEntity<String> enableAuthResp = restTemplate.postForEntity("http://localhost:8001/apis/stock-api/plugins", new HttpEntity<>(authPlugin), String.class); |
| 103 | + |
| 104 | + assertTrue(HttpStatus.CREATED == enableAuthResp.getStatusCode() || HttpStatus.CONFLICT == enableAuthResp.getStatusCode()); |
| 105 | + |
| 106 | + String pluginsResp = restTemplate.getForObject("http://localhost:8001/apis/stock-api/plugins", String.class); |
| 107 | + assertTrue(pluginsResp.contains("key-auth")); |
| 108 | + |
| 109 | + HttpHeaders headers = new HttpHeaders(); |
| 110 | + headers.set("Host", "stock.api"); |
| 111 | + RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); |
| 112 | + ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class); |
| 113 | + assertEquals(HttpStatus.UNAUTHORIZED, stockPriceResp.getStatusCode()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void givenAPIAuthEnabled_whenAddKey_thenAccessAllowed() throws Exception { |
| 118 | + String apiListResp = restTemplate.getForObject("http://localhost:8001/apis/", String.class); |
| 119 | + if (!apiListResp.contains("stock-api")) { |
| 120 | + givenKongAdminAPI_whenAddAPI_thenAPIAccessibleViaKong(); |
| 121 | + } |
| 122 | + |
| 123 | + String consumerListResp = restTemplate.getForObject("http://localhost:8001/consumers/", String.class); |
| 124 | + if (!consumerListResp.contains("eugenp")) { |
| 125 | + givenKongAdminAPI_whenAddAPIConsumer_thenAdded(); |
| 126 | + } |
| 127 | + |
| 128 | + final String consumerKey = "eugenp.pass"; |
| 129 | + KeyAuthObject keyAuth = new KeyAuthObject(consumerKey); |
| 130 | + ResponseEntity<String> keyAuthResp = restTemplate.postForEntity("http://localhost:8001/consumers/eugenp/key-auth", new HttpEntity<>(keyAuth), String.class); |
| 131 | + |
| 132 | + assertTrue(HttpStatus.CREATED == keyAuthResp.getStatusCode() || HttpStatus.CONFLICT == keyAuthResp.getStatusCode()); |
| 133 | + |
| 134 | + HttpHeaders headers = new HttpHeaders(); |
| 135 | + headers.set("Host", "stock.api"); |
| 136 | + headers.set("apikey", consumerKey); |
| 137 | + RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); |
| 138 | + ResponseEntity<String> stockPriceResp = restTemplate.exchange(requestEntity, String.class); |
| 139 | + |
| 140 | + assertEquals("10000", stockPriceResp.getBody()); |
| 141 | + |
| 142 | + headers.set("apikey", "wrongpass"); |
| 143 | + requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("http://localhost:8000/stock/btc")); |
| 144 | + stockPriceResp = restTemplate.exchange(requestEntity, String.class); |
| 145 | + assertEquals(HttpStatus.FORBIDDEN, stockPriceResp.getStatusCode()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void givenAdminAPIProxy_whenAddAPIViaProxy_thenAPIAdded() throws Exception { |
| 150 | + APIObject adminAPI = new APIObject("admin-api", "admin.api", "http://localhost:8001", "/admin-api"); |
| 151 | + HttpEntity<APIObject> apiEntity = new HttpEntity<>(adminAPI); |
| 152 | + ResponseEntity<String> addAPIResp = restTemplate.postForEntity("http://localhost:8001/apis", apiEntity, String.class); |
| 153 | + |
| 154 | + assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode()); |
| 155 | + |
| 156 | + HttpHeaders headers = new HttpHeaders(); |
| 157 | + headers.set("Host", "admin.api"); |
| 158 | + APIObject baeldungAPI = new APIObject("baeldung-api", "baeldung.com", "http://ww.baeldung.com", "/"); |
| 159 | + RequestEntity<APIObject> requestEntity = new RequestEntity<>(baeldungAPI, headers, HttpMethod.POST, new URI("http://localhost:8000/admin-api/apis")); |
| 160 | + addAPIResp = restTemplate.exchange(requestEntity, String.class); |
| 161 | + |
| 162 | + assertTrue(HttpStatus.CREATED == addAPIResp.getStatusCode() || HttpStatus.CONFLICT == addAPIResp.getStatusCode()); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments