Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions example-app/test-app/sinatra/robolectric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' &&
Expand All @@ -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

Original file line number Diff line number Diff line change
@@ -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");
}
}
}
6 changes: 6 additions & 0 deletions src/com/roobit/android/restclient/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down