Search in sources :

Example 1 with HTTPHeader

use of com.github.ljtfreitas.julian.http.HTTPHeader in project julian-http-client by ljtfreitas.

the class HeadersResponseTTest method compose.

@Test
void compose(@Mock ResponseFn<Void, Void> fn, @Mock HTTPResponse<Void> response) {
    Arguments arguments = Arguments.empty();
    Headers headers = Headers.create(new Header("X-Header", "x-header-content"));
    when(response.headers()).thenReturn(headers.all().stream().reduce(HTTPHeaders.empty(), (a, b) -> a.join(new HTTPHeader(b.name(), b.values())), (a, b) -> b));
    when(response.cast(notNull())).thenCallRealMethod();
    Headers actual = responseT.bind(endpoint, fn).join(Promise.done(response), arguments);
    assertThat(actual, contains(headers.all().toArray()));
}
Also used : MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mock(org.mockito.Mock) Mockito.when(org.mockito.Mockito.when) Nested(org.junit.jupiter.api.Nested) Test(org.junit.jupiter.api.Test) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPResponse(com.github.ljtfreitas.julian.http.HTTPResponse) Matchers.contains(org.hamcrest.Matchers.contains) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.notNull(org.mockito.ArgumentMatchers.notNull) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) Test(org.junit.jupiter.api.Test)

Example 2 with HTTPHeader

use of com.github.ljtfreitas.julian.http.HTTPHeader in project julian-http-client by ljtfreitas.

the class WebClientHTTPRequest method read.

Mono<HTTPResponse<T>> read(ClientResponse response) {
    HTTPStatus status = new HTTPStatus(response.rawStatusCode(), response.statusCode().getReasonPhrase());
    HTTPHeaders headers = response.headers().asHttpHeaders().entrySet().stream().reduce(HTTPHeaders.empty(), (h, e) -> h.join(new HTTPHeader(e.getKey(), e.getValue())), (a, b) -> b);
    if (response.statusCode().isError()) {
        return response.createException().map(e -> failure(status, headers, e));
    } else {
        return success(status, headers, response);
    }
}
Also used : HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader)

Example 3 with HTTPHeader

use of com.github.ljtfreitas.julian.http.HTTPHeader in project julian-http-client by ljtfreitas.

the class DebugHTTPClientTest method show.

@Test
void show(@Mock HTTPRequest<String> httpRequest) {
    mockServer.when(request("/debug").withMethod("POST").withBody("request body")).respond(response().withStatusCode(200).withContentType(TEXT_PLAIN).withHeader("x-some-header", "some-content").withBody("it works!"));
    DebugHTTPClient debugHTTPClient = new DebugHTTPClient(new DefaultHTTPClient());
    when(httpRequest.path()).thenReturn(URI.create("http://localhost:8090/debug"));
    when(httpRequest.method()).thenReturn(HTTPMethod.POST);
    when(httpRequest.headers()).thenReturn(new HTTPHeaders(List.of(new HTTPHeader("X-Some-Header", "some-content"), new HTTPHeader("Accept", "text/plain"))));
    when(httpRequest.body()).thenReturn(Optional.of(new DefaultHTTPRequestBody(MediaType.TEXT_PLAIN, () -> BodyPublishers.ofString("request body"))));
    HTTPClientRequest intercepted = debugHTTPClient.request(httpRequest);
    intercepted.execute().join().unsafe();
    verify(httpRequest, atLeastOnce()).path();
    verify(httpRequest, atLeastOnce()).headers();
    verify(httpRequest, atLeastOnce()).method();
}
Also used : HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) DefaultHTTPRequestBody(com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) Test(org.junit.jupiter.api.Test)

Aggregations

HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)3 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)3 Test (org.junit.jupiter.api.Test)2 DefaultHTTPRequestBody (com.github.ljtfreitas.julian.http.DefaultHTTPRequestBody)1 HTTPResponse (com.github.ljtfreitas.julian.http.HTTPResponse)1 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.contains (org.hamcrest.Matchers.contains)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Nested (org.junit.jupiter.api.Nested)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 ArgumentMatchers.notNull (org.mockito.ArgumentMatchers.notNull)1 Mock (org.mockito.Mock)1 Mockito.when (org.mockito.Mockito.when)1 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)1