質問

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;
    } 
(...)
}
役に立ちましたか?

解決 2

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");
        }
    });

他のヒント

404 is for resource http://10.17.1.72:8080/contacts not found

WIKIPEDIA: 404 Not Found The requested resource could not be found but may be available again in the future. Subsequent requests by the client are permissible.

if resource is there and proper input is not give for process

it will not give 404! Read More

plus as per following code, there are 2 things

  1. you misspelled limite
  2. you are comparing received values to Strings like order offset amd limit

i do not think that is what you want to achieve, you should be comparing it with some variables rather

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

Have you tracked the HTTP request using tools and does the request reaches your handler before throwing the error?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top