@Test public void testMethod() throws IOException { handler = (request, response) -> response.setBody(request.method().name()); System.out.println("get"); Assert.assertEquals("GET", makeRequest("/").get().asString()); System.out.println("post"); Assert.assertEquals("POST", makeRequest("/").post().asString()); System.out.println("put"); Assert.assertEquals("PUT", makeRequest("/").method(ConnectionMethod.PUT).execute().asString()); System.out.println("delete"); Assert.assertEquals("DELETE", makeRequest("/").method(ConnectionMethod.DELETE).execute().asString()); }
public Request createRequest(String url) { Request request = new Request(url) .addHeaders(headers) .addParameterLists(params) .setPauseBetweenRetries(pauseBetweenRetries) .setMaxRetryCount(maxRetryCount) .setProxy(proxy) .setSSLSocketFactory(sslFactory) .setHostnameVerifier(hostnameVerifier) .method(method) .connectTimeout(connectTimeout) .readTimeout(readTimeout) .setUrlEncoding(charsetForEncoding); request.data = data; request.dataTypes = dataTypes; request.dataArrays = dataArrays; request.redirects = redirects; request.multipart = multipart; return request; }
public Request createRequest(String url) { Request request = new Request(url) .addHeaders(headers) .addParameterLists(params) .setPauseBetweenRetries(pauseBetweenRetries) .setMaxRetryCount(maxRetryCount) .setProxy(proxy) .setSSLSocketFactory(sslFactory) .setHostnameVerifier(hostnameVerifier) .method(method) .connectTimeout(connectTimeout) .readTimeout(readTimeout) .setUrlEncoding(charsetForEncoding); request.data = data; request.dataTypes = dataTypes; request.dataArrays = dataArrays; request.redirects = redirects; request.multipart = multipart; return request; }
@Test public void test_allowNothing_butOptions() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler().get(null)) ; Assert.assertEquals(405, makeRequest("/rest").get().getResponseCode()); Assert.assertEquals("OPTIONS", makeRequest("/rest").get().header("Allow")); Assert.assertEquals(405, makeRequest("/rest").method(ConnectionMethod.HEAD).execute().getResponseCode()); }
@Test public void test_allowGetAndDelete() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler() .get( (request, response) -> response.setBody("get") ) .delete( (request, response) -> response.setBody("delete") )) ; Assert.assertEquals("get", makeRequest("/rest").get().asString()); Assert.assertEquals("delete", makeRequest("/rest").method(ConnectionMethod.DELETE).execute().asString()); Assert.assertEquals(405, makeRequest("/rest").post().getResponseCode()); Assert.assertEquals("GET, HEAD, DELETE, OPTIONS", makeRequest("/rest").post().header("Allow")); }
@Test public void test_allowPostAndPut() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler() .post( (request, response) -> response.setBody("post") ) .put( (request, response) -> response.setBody("put") )) ; Assert.assertEquals("post", makeRequest("/rest").post().asString()); Assert.assertEquals("put", makeRequest("/rest").method(ConnectionMethod.PUT).execute().asString()); Assert.assertEquals(405, makeRequest("/rest").get().getResponseCode()); Assert.assertEquals("POST, PUT, OPTIONS", makeRequest("/rest").get().header("Allow")); Assert.assertEquals("POST, PUT, OPTIONS", makeRequest("/rest").options().header("Allow")); }