Skip to content

Commit a7abe6c

Browse files
[JENKINS-76231] Fix NullPointerException when Content-Type header is missing (#219)
* Fix NullPointerException when handling responses without Content-Type header * Add test case for handling responses without Content-Type header
1 parent bc2abb9 commit a7abe6c

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/main/java/jenkins/plugins/http_request/ResponseContentSupplier.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ public InputStream getContentStream() {
124124
private void readCharset(ClassicHttpResponse response) {
125125
Charset charset = null;
126126

127+
Header contentTypeHeader = response.getFirstHeader(HttpHeaders.CONTENT_TYPE);
127128
ContentType contentType = ContentType.parse(response.getEntity() != null ?
128129
response.getEntity().getContentType() :
129-
response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
130+
(contentTypeHeader != null ? contentTypeHeader.getValue() : null));
130131
if (contentType != null) {
131132
charset = contentType.getCharset();
132133
if (charset == null) {

src/test/java/jenkins/plugins/http_request/HttpRequestTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,18 @@ void responseContentSupplierHeadersCaseInsensitivity() throws Exception {
990990
respSupplier.close();
991991
}
992992

993+
@Test
994+
void responseContentSupplierHandlesNoContentTypeHeader() throws Exception {
995+
// Prepare test context - 204 No Content response without Content-Type header
996+
CloseableHttpResponse response = HttpResponseAdapter.adapt(new BasicClassicHttpResponse(204, "No Content"));
997+
// Don't set entity or Content-Type header to simulate GitHub API 204 response
998+
// Run test
999+
ResponseContentSupplier respSupplier = new ResponseContentSupplier(ResponseHandle.NONE, response);
1000+
// Check expectations - should not throw NullPointerException
1001+
assertEquals(204, respSupplier.getStatus());
1002+
respSupplier.close();
1003+
}
1004+
9931005
@Test
9941006
void testFileUpload() throws Exception {
9951007
// Prepare the server

0 commit comments

Comments
 (0)