eclipse-ee4j / eclipse-ee4j/jersey

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

Open
#3,776 3 comments 0 reactions 0 assignees View on GitHub
Jersey 1.x
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

file `jersey-1.19.1-PUT-401-bug.groovy`
```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)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.