Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty response body from 401 response caused by PUT with payload. Jersey 1.19.1. #3776

Open
jerseyrobot opened this issue Feb 23, 2018 · 3 comments

Comments

@jerseyrobot
Copy link
Contributor

file jersey-1.19.1-PUT-401-bug.groovy

@groovy.lang.Grab('com.sun.jersey:jersey-bundle:1.19.1')
import com.sun.jersey.api.client.Client
import com.sun.jersey.api.client.ClientHandlerException
import com.sun.jersey.api.client.ClientRequest
import com.sun.jersey.api.client.ClientResponse
import com.sun.jersey.api.client.UniformInterfaceException
import com.sun.jersey.api.client.filter.ClientFilter

// If PUT or POST request with any payload will cause 401 response
// then ClientFilter in Jersey 1.* is not able to get response
// body.
// Run with: $ groovy jersey-1.19.1-PUT-401-bug.groovy
// Require: nc - unix netcat utility

// Setup server
def responseFile = File.createTempFile("test-nc-response", ".txt")
responseFile.text = """HTTP/1.1 401 Unauthorized
Content-length: 8
Connection: close
Content-Type: text/plain

response"""
def nc = new ProcessBuilder('nc', '-l', '5530')
        .redirectInput(responseFile)
        .start()

// Setup client with filter to catch response body
def responseBody = null
def client = Client.newInstance()
client.addFilter(new ClientFilter() {
    ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
        def response = getNext().handle(cr)
        responseBody = response.getEntity(String.class)
        return response;
    }
})

try {
    client.resource("http://localhost:5530")
            .put(String.class, "".getBytes())
    throw new RuntimeException(".put() call expected to throw UniformInterfaceException because of 401 response, but it did not!")
} catch (UniformInterfaceException e) {
    assert "PUT http://localhost:5530 returned a response status of 401 Unauthorized" == e.getMessage()
}
nc.waitFor()
responseFile.delete()

assert "response" == responseBody
println "OK!"

Expected to output OK!, but fails with

Assertion failed: 

assert "response" == responseBody
                  |  |
                  |  ""
                  false

	at jersey-1_19_1-PUT-401-bug.run(jersey-1.19.1-PUT-401-bug.groovy:49)
@jerseyrobot
Copy link
Contributor Author

@jansupol Commented
This is actually Jersey 2 Issue tracker. For Jersey 1.x (https://github.com/jersey/jersey-1.x), there is currently no plan on a new release.

@jerseyrobot
Copy link
Contributor Author

@yueki1993
Copy link

I got almost the same bug with this issue, using JerseyTest 1.x
A workaround of this issue with JerseyTest is to use 3rd party http client libs, and just use WebResource for getting the endpoint of APIs, i.e., (resource().path("foo/bar").getURI())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants