jcabi-http icon indicating copy to clipboard operation
jcabi-http copied to clipboard

RestResponse.assertBody behavior is broken

Open paulodamaso opened this issue 7 years ago • 38 comments

RestResponse.assertBody behavior is broken: it is checking all the Response string when it should only check the body part of string.

This makes impossible for testing when we got an empty message body from server, because assertBody considers header section too when asserting if message body has some content.

Please refer to FtRemotesTest on https://github.com;yegor256/takes to see how this method is misbehaving.

paulodamaso avatar Sep 24 '18 12:09 paulodamaso

@yegor256/z please, pay attention to this issue

0crat avatar Sep 24 '18 12:09 0crat

@paulodamaso/z this project will fix the problem faster if you donate a few dollars to it; just click here and pay via Stripe, it's very fast, convenient and appreciated; thanks a lot!

0crat avatar Sep 24 '18 12:09 0crat

@yegor256 @paulodamaso after some investigation (for yegor256/takes#800), I think the problem is not with assertBody but with the way body is parsed by jcabi-http.

Take those four examples (using takes).

Expected empty body: it fails, because the body, according to RestResponse, is HTTP/1.1 200 OK\r\n\r\n:

    @Test
    public void returnsAnEmptyResponseBody() throws IOException {
        new FtRemote(
            new TkEmpty()
        ).exec(
            new FtRemote.Script() {
                @Override
                public void exec(final URI home) throws IOException {
                    new JdkRequest(home)
                        .method("POST")
                        .body().set("returnsAnEmptyResponseBody").back()
                        .fetch()
                        .as(RestResponse.class)
                        .assertBody(new IsEqual<>(""))
                        .assertStatus(HttpURLConnection.HTTP_OK);
                }
            }
        );
    }

The same with ApacheRequest fails in the same way.

    @Test
    public void returnsANonEmptyResponseBody() throws IOException {
        new FtRemote(
            new TkText("test")
        ).exec(
            new FtRemote.Script() {
                @Override
                public void exec(final URI home) throws IOException {
                    new JdkRequest(home)
                        .method("POST")
                        .body().set("returnsAnEmptyResponseBody").back()
                        .fetch()
                        .as(RestResponse.class)
                        .assertBody(new IsEqual<>("test"))
                        .assertStatus(HttpURLConnection.HTTP_OK);
                }
            }
        );
    }

Now expecting a body, it's even worse, the returned body is: testHTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: text/plain\r\n\r\ntest!

    @Test
    public void returnsANonEmptyResponseBody() throws IOException {
        new FtRemote(
            new TkText("test")
        ).exec(
            new FtRemote.Script() {
                @Override
                public void exec(final URI home) throws IOException {
                    new JdkRequest(home)
                        .method("POST")
                        .body().set("returnsAnEmptyResponseBody").back()
                        .fetch()
                        .as(RestResponse.class)
                        .assertBody(new IsEqual<>("test"))
                        .assertStatus(HttpURLConnection.HTTP_OK);
                }
            }
        );
    }

But if you use ApacheRequest, then everything works as expected!

    @Test
    public void returnsANonEmptyResponseBody() throws IOException {
        new FtRemote(
            new TkText("test")
        ).exec(
            new FtRemote.Script() {
                @Override
                public void exec(final URI home) throws IOException {
                    new ApacheRequest(home)
                        .method("POST")
                        .body().set("returnsAnEmptyResponseBody").back()
                        .fetch()
                        .as(RestResponse.class)
                        .assertBody(new IsEqual<>("test"))
                        .assertStatus(HttpURLConnection.HTTP_OK);
                }
            }
        );
    }

I have also tested the same test with rest-assured instead of jcabi-http, and it passes without any problem.

victornoel avatar Nov 18 '18 09:11 victornoel

@yegor256 ping

paulodamaso avatar Nov 18 '18 18:11 paulodamaso

@yegor256 What do you think about it? If this ticket is valid please put it on scope, we have a ticket on takes that depends on it.

paulodamaso avatar Nov 19 '18 16:11 paulodamaso

@paulodamaso done

yegor256 avatar Dec 25 '18 15:12 yegor256

Job #177 is now in scope, role is DEV

0crat avatar Dec 25 '18 15:12 0crat

Bug was reported, see §29: +15 point(s) just awarded to @paulodamaso/z

0crat avatar Dec 25 '18 15:12 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Dec 25 '18 15:12 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Dec 30 '18 16:12 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 04 '19 17:01 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 09 '19 18:01 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 14 '19 19:01 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 19 '19 20:01 0crat

@yegor256 assign to me

fabriciofx avatar Jan 20 '19 04:01 fabriciofx

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 24 '19 21:01 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Jan 29 '19 22:01 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Feb 03 '19 23:02 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Feb 09 '19 00:02 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Feb 14 '19 01:02 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Feb 19 '19 02:02 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Feb 24 '19 03:02 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Mar 01 '19 04:03 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Mar 06 '19 05:03 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Mar 11 '19 06:03 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Mar 16 '19 07:03 0crat

@yegor256 Please pay attention to this issue. Please see We need this issue be resolved to fix tests in Takes project

fevzian avatar Nov 14 '19 20:11 fevzian

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Nov 18 '19 18:11 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Nov 23 '19 19:11 0crat

@yegor256/z everybody who has role DEV is banned at #177; I won't be able to assign anyone automatically; consider assigning someone manually (as in §19), or invite more people (as in §51), or remove the job from the scope (as in §14)

0crat avatar Nov 28 '19 20:11 0crat