From aeb2d9e3cb7da829a7fcd5f49d7ba2a81360a8ca Mon Sep 17 00:00:00 2001 From: Eldad Date: Mon, 10 Dec 2012 17:15:26 -0800 Subject: [PATCH] added PUT test --- example-app/test-app/sinatra/robolectric.rb | 28 +++- .../exampleapp/RestClientPutTest.java | 146 ++++++++++++++++++ .../roobit/android/restclient/RestClient.java | 6 + 3 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 example-app/tests/com/roobit/android/restclient/exampleapp/RestClientPutTest.java diff --git a/example-app/test-app/sinatra/robolectric.rb b/example-app/test-app/sinatra/robolectric.rb index 2481195..738211e 100644 --- a/example-app/test-app/sinatra/robolectric.rb +++ b/example-app/test-app/sinatra/robolectric.rb @@ -18,10 +18,6 @@ env['HTTP_X_SOME_SPECIAL_HEADER'] end -patch '/test_endpoint/should_patch' do - halt 400 unless request.patch? -end - post '/test_endpoint/should_post_form' do halt 400 unless params[:username] == 'assad' && @@ -32,3 +28,27 @@ data = JSON.parse request.body.read halt 400 unless data["id"] == 42 end + +patch '/test_endpoint/should_patch' do + halt 400 unless request.patch? +end + +put '/test_endpoint/echo_request_method' do + request.request_method +end + +put '/test_endpoint/awesome_header' do + env['HTTP_X_SOME_SPECIAL_HEADER'] +end + +put '/test_endpoint/should_put_form' do + halt 400 unless + params[:username] == 'assad' && + params[:password] == '12345' +end + +put '/test_endpoint/should_put_data' do + data = JSON.parse request.body.read + halt 400 unless data["id"] == 42 +end + diff --git a/example-app/tests/com/roobit/android/restclient/exampleapp/RestClientPutTest.java b/example-app/tests/com/roobit/android/restclient/exampleapp/RestClientPutTest.java new file mode 100644 index 0000000..3e597a1 --- /dev/null +++ b/example-app/tests/com/roobit/android/restclient/exampleapp/RestClientPutTest.java @@ -0,0 +1,146 @@ +package com.roobit.android.restclient.exampleapp; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.roobit.android.restclient.RestClient; +import com.roobit.android.restclient.RestResult; +import com.xtremelabs.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public class RestClientPutTest { + + static final String TEST_ENDPOINT = "http://localhost:4567/test_endpoint"; + + RestResult restResult; + + @Test + public void shouldPut() throws Exception { + final CountDownLatch latch = new CountDownLatch(1); + RestClient.clientWithBaseUrl(TEST_ENDPOINT) + .setResource("echo_request_method") + .put() + .execute(new RestClient.OnCompletionListener() { + @Override + public void success(RestClient client, RestResult result) { + restResult = result; + latch.countDown(); + } + + @Override + public void failedWithError(RestClient restClient, int responseCode, RestResult result) { + restResult = result; + latch.countDown(); + } + }); + + if (latch.await(5, TimeUnit.SECONDS)) { + assertThat(restResult.getResponse(), equalTo("PUT")); + } + else { + fail("Timed out waiting for PUT"); + } + } + + @Test + public void shouldPutHttpHeaders() throws Exception { + Properties httpHeaders = new Properties(); + httpHeaders.put("X-Some-Special-Header", "awesomeness"); + + final CountDownLatch latch = new CountDownLatch(1); + RestClient.clientWithBaseUrl(TEST_ENDPOINT) + .setResource("awesome_header") + .put(httpHeaders) + .execute(new RestClient.OnCompletionListener() { + @Override + public void success(RestClient client, RestResult result) { + restResult = result; + latch.countDown(); + } + + @Override + public void failedWithError(RestClient restClient, int responseCode, RestResult result) { + restResult = result; + latch.countDown(); + } + }); + + if (latch.await(5, TimeUnit.SECONDS)) { + assertThat(restResult.getResponse(), equalTo("awesomeness")); + } + else { + fail("Timed out waiting for PUT"); + } + } + + @Test + public void shouldPutFormData() throws Exception { + Properties parameters = new Properties(); + parameters.setProperty("username", "assad"); + parameters.setProperty("password", "12345"); + + final CountDownLatch latch = new CountDownLatch(1); + RestClient.clientWithBaseUrl(TEST_ENDPOINT) + .setResource("should_put_form") + .putForm(parameters) + .execute(new RestClient.OnCompletionListener() { + @Override + public void success(RestClient client, RestResult result) { + restResult = result; + latch.countDown(); + } + + @Override + public void failedWithError(RestClient restClient, int responseCode, RestResult result) { + restResult = result; + latch.countDown(); + } + }); + + if (latch.await(5, TimeUnit.SECONDS)) { + assertTrue("PUT form failed", restResult.isSuccess()); + } + else { + fail("Timed out waiting for PUT"); + } + } + + @Test + public void shouldPutData() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write("{\"id\": 42}".getBytes()); + + final CountDownLatch latch = new CountDownLatch(1); + RestClient.clientWithBaseUrl(TEST_ENDPOINT) + .setResource("should_put_data") + .put(baos) + .execute(new RestClient.OnCompletionListener() { + @Override + public void success(RestClient client, RestResult result) { + restResult = result; + latch.countDown(); + } + + @Override + public void failedWithError(RestClient restClient, int responseCode, RestResult result) { + restResult = result; + latch.countDown(); + } + }); + + if (latch.await(5, TimeUnit.SECONDS)) { + assertTrue("PUT data failed", restResult.isSuccess()); + } + else { + fail("Timed out waiting for PUT data"); + } + } +} diff --git a/src/com/roobit/android/restclient/RestClient.java b/src/com/roobit/android/restclient/RestClient.java index 19150d6..66232f2 100644 --- a/src/com/roobit/android/restclient/RestClient.java +++ b/src/com/roobit/android/restclient/RestClient.java @@ -230,6 +230,12 @@ public RestClient put(ByteArrayOutputStream postData, String contentType, Proper return this; } + public RestClient putForm(Properties parameters) { + put(); + setParameters(parameters); + return this; + } + private void setHttpHeaders(Properties httpHeaders) { this.httpHeaders = httpHeaders; }