Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

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

Open
SemyenSoldatenko opened this issue Feb 23, 2018 · 1 comment

Comments

@SemyenSoldatenko
Copy link

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)
@jansupol
Copy link
Contributor

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.

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

No branches or pull requests

2 participants