Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package org.eclipse.sw360.http;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.util.function.Consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -44,7 +44,6 @@ class NewRequestBuilderImpl implements RequestBuilder {
/**
* The body of the request.
*/

private BodyPublisher body;

/**
Expand All @@ -67,11 +66,7 @@ public RequestBuilder method(Method method) {

@Override
public RequestBuilder uri(String uri) {
try {
requestBuilder.uri(new URI(uri));
} catch (URISyntaxException e) {
// nothing to do
}
requestBuilder.uri(URI.create(uri));
return this;
}

Expand Down Expand Up @@ -110,8 +105,8 @@ public RequestBuilder multiPart(String name, Consumer<RequestBodyBuilder> partPr
* @return the request constructed by this builder
*/
public HttpRequest build() {
BodyPublisher requestBody = getBody();
return requestBuilder.method(httpMethod, requestBody).header("Content-Type", "application/json").build();
BodyPublisher requestBody = (getBody() != null) ? getBody() : BodyPublishers.noBody();
return requestBuilder.method(httpMethod, requestBody).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Bosch.IO GmbH 2020.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.http;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;

import java.net.http.HttpRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* Unit test class for {@code NewRequestBuilderImpl}. This class focuses on
* request-building semantics for the native HttpClient variant.
*/
public class NewRequestBuilderImplTest {
/**
* The builder to be tested.
*/
private NewRequestBuilderImpl requestBuilder;

@Before
public void setUp() {
ObjectMapper mapper = mock(ObjectMapper.class);
requestBuilder = new NewRequestBuilderImpl(mapper);
}

@Test(expected = IllegalArgumentException.class)
public void testInvalidUriRejected() {
requestBuilder.uri("://invalid-uri");
}

@Test
public void testBuildGetWithoutBody() {
requestBuilder.uri("http://example.org/releases");
HttpRequest request = requestBuilder.build();

assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().firstValue("Content-Type")).isEmpty();
}

@Test
public void testBuildPostWithoutBody() {
requestBuilder.uri("http://example.org/releases");
requestBuilder.method(RequestBuilder.Method.POST);
HttpRequest request = requestBuilder.build();

assertThat(request.method()).isEqualTo("POST");
assertThat(request.headers().firstValue("Content-Type")).isEmpty();
}

@Test
public void testDoesNotOverrideExplicitContentType() {
requestBuilder.uri("http://example.org/releases");
requestBuilder.header("Content-Type", "text/plain");
requestBuilder.body(body -> body.string("payload", "text/plain"));
HttpRequest request = requestBuilder.build();

assertThat(request.headers().firstValue("Content-Type")).hasValue("text/plain");
}
}
Loading