Search code examples
javajunithttprequesthttpresponserestlet

Should have a 404 response to a GET request but received 200. Restlet


I've a RESTlet server running on my android device. It is configured fine. Now I'm jUnit testing it and this particular test is failing for no reason.

I've this URL:

http://10.17.1.72:8080/contacts?order=ASC&limit=10&offset=1 which is correct, I receive a 200 response as expected.

But then if I misspell the parameters in the URL I should get a 404 response. Using postman extension for Chrome if I hit http://10.17.1.72:8080/contacts?oooooooorder=ASC&limit=10&offset=1 (note that "order" is misspelled) I receive a 404 as I should. Until here everything is fine.

The problem comes when I create a RESTlet client to make that GET request on my jUnit test, it receives a 200 response.

Here is my jUnit test method:

public void testGoodRequest() // Success, receives a 200 code.
{
    // Create the client resource
    ClientResource resource = new ClientResource("http://10.17.1.72:8080/contacts?order=ASC&limit=10&offset=1");
    Response response = resource.getResponse();

    Log.d(TAG, "Good: " + response.getStatus().getCode());

    assertTrue(response.getStatus().getCode() == 200);      
}

Adn this one should receive a 404 but receives a 200, eventho the same get request using Chrome's postman receives a 404:

public void testBadRequestWithOrderMisspelled()
{
    // Create the client resource
    ClientResource resource = new ClientResource("http://10.17.1.72:8080/contacts?oofdgrder=ASC&limit=10&offset=1");
    Response response = resource.getResponse();

    Log.d(TAG, "BadRequestWithOrderMisspelled: " + response.getStatus().getCode());

    assertTrue(response.getStatus().getCode() == 404); // Assert fails, receives 200 instead of 404     
}

And here is my Restlet handle method:

@Override
public void handle(Request request, Response response) {

    //final ContactList contactList = new ContactList(mContext);
    String type = request.getMethod().getName();

    String order = request.getResourceRef().getQueryAsForm().getFirstValue("order");
    String limit = request.getResourceRef().getQueryAsForm().getFirstValue("limit");
    String offset = request.getResourceRef().getQueryAsForm().getFirstValue("offset");
    String query = request.getResourceRef().getQueryAsForm().getFirstValue("query");

    if(!"order".equals(order) || !"limit".equals(limit) || !"offset".equals(offset) || !"query".equals(query))
    {
        // Show error
        response.setStatus(new Status(Status.CLIENT_ERROR_NOT_FOUND, "Badly formatted URL."));
        return;
    } 
(...)
}

Solution

  • For some reason that I don't know RESTlet client was probably the reason for my problem.

    I switched over to an android async http client and things are running smoothly.

    Here is my code using async http client:

        SyncHttpClient client = new SyncHttpClient();
    
        // Should be success
        client.get("http://10.17.1.72:8080/contacts", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                Log.d(TAG, "GET - ID: 1 - Success expected -> Got success");
                auxArray.add("1");
            }
    
            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                super.onFailure(arg0, arg1, arg2, arg3);
                Log.d(TAG, "GET - ID: 1 - Success expected -> Got failure");
            }
        });