private static Optional<String> printableRequestBody(final HttpRequestIntent httpRequest) { return Optional.ofNullable(httpRequest.getBody()) .map(body -> { final String bodyAsString; if (httpRequest.getBody() instanceof StringHttpRequestBody) { bodyAsString = ((StringHttpRequestBody) httpRequest.getBody()).getSecuredBody(); } else if (httpRequest.getBody() instanceof FormUrlEncodedHttpRequestBody) { bodyAsString = ((FormUrlEncodedHttpRequestBody) httpRequest.getBody()).getParameters().stream() .map(pair -> pair.getName() + "=" + pair.getValue()) .collect(Collectors.joining("&")); } else { bodyAsString = "**omitted output**"; } return bodyAsString; }); } }
private String requestBodyFormatted() { try { final Optional<String> stringBodyOfHttpRequest = stringBodyOfHttpRequest(); final Optional<String> stringBodyOfHttpRequestIntentSupplier = Optional.ofNullable(httpRequestIntent) .map(r -> r.getBody()) .filter(r -> r instanceof StringHttpRequestBody) .map(r -> ((StringHttpRequestBody) r).getSecuredBody()); return Optional.ofNullable(stringBodyOfHttpRequest.orElse(stringBodyOfHttpRequestIntentSupplier.orElse(null))) .map(SphereJsonUtils::prettyPrint) .map(s -> "http request formatted body: " + s + "\n") .orElse(""); } catch (final Exception e) { return ""; } }
public HttpRequestIntent withHeaders(final HttpHeaders headers) { return HttpRequestIntent.of(getHttpMethod(), path, headers, getBody()); }
public HttpRequestIntent withPath(final String path) { return HttpRequestIntent.of(getHttpMethod(), path, getHeaders(), getBody()); }
public HttpRequestIntent prefixPath(final String prefix) { return HttpRequestIntent.of(getHttpMethod(), prefix + getPath(), getHeaders(), getBody()); }
@Test public void priceSelectionParameterCanBeAdded() { assertThat(SEARCH_WITH_FULL_PRICE_SELECTION.httpRequestIntent().getBody().toString()) .contains("priceCurrency=EUR&priceCountry=DE&priceCustomerGroup=customer-group-id&priceChannel=channel-id"); assertThat(SEARCH_WITH_FULL_PRICE_SELECTION.getPriceSelection()).isEqualTo(FULL_PRICE_SELECTION); }
public HttpRequest toHttpRequest(final String baseUrl) { return HttpRequest.of(getHttpMethod(), baseUrl + getPath(), getHeaders(), getBody()); }
public HttpRequest toHttpRequest(final String baseUrl) { return HttpRequest.of(getHttpMethod(), baseUrl + getPath(), getHeaders(), getBody()); }
public HttpRequestIntent prefixPath(final String prefix) { return HttpRequestIntent.of(getHttpMethod(), prefix + getPath(), getHeaders(), getBody()); }
public HttpRequestIntent plusHeader(final String name, final String value) { return HttpRequestIntent.of(getHttpMethod(), getPath(), getHeaders().plus(name, value), getBody()); }
public HttpRequestIntent plusHeader(final String name, final String value) { return HttpRequestIntent.of(getHttpMethod(), getPath(), getHeaders().plus(name, value), getBody()); }
@Test public void priceSelectionCanBeRefined() { final PriceSelectionDsl priceSelectionWithoutCustomerGroup = FULL_PRICE_SELECTION.withPriceCustomerGroup(null); final ProductProjectionSearch priceSearchWithoutCustomer = SEARCH_WITH_FULL_PRICE_SELECTION.withPriceSelection(priceSelectionWithoutCustomerGroup); assertThat(priceSearchWithoutCustomer.httpRequestIntent().getBody().toString()) .contains("priceCurrency=EUR&priceCountry=DE&priceChannel=channel-id") .doesNotContain("priceCustomerGroup").doesNotContain("customer-group-id"); assertThat(priceSearchWithoutCustomer.getPriceSelection()).isEqualTo(priceSelectionWithoutCustomerGroup); }
@Test public void unicode() throws Exception { final StringHttpRequestBody body = (StringHttpRequestBody) ProductProjectionSearch.ofStaged().withText(GERMAN, "öón").httpRequestIntent().getBody(); final String path = body.getString(); final String expected = "text.de=%C3%B6%C3%B3n&staged=true"; assertThat(path).isEqualTo(expected); }
@Test public void removePriceSelection() { final ProductProjectionSearch priceSelectionRemoved = SEARCH_WITH_FULL_PRICE_SELECTION.withPriceSelection(null); assertThat(priceSelectionRemoved.httpRequestIntent().getBody().toString()).doesNotContain("price"); assertThat(priceSelectionRemoved.getPriceSelection()).isNull(); }
@Test public void channelsFilterDsl() { final StringHttpRequestBody body = (StringHttpRequestBody) ProductProjectionSearch.ofStaged().plusQueryFilters(m -> m.allVariants().availability().channels().channelId("channel-id-12") .availableQuantity().isGreaterThanOrEqualTo(new BigDecimal(3))).httpRequestIntent().getBody(); assertThat(body.getString()).contains("filter.query=variants.availability.channels.channel-id-12.availableQuantity%3Arange%283+to+*%29"); }
@Test public void channelsRestockableSortDsl() { final StringHttpRequestBody body = (StringHttpRequestBody) ProductProjectionSearch.ofStaged(). plusSort(m -> m.allVariants().availability().channels().channelId("channel-id-500").restockableInDays().asc()).httpRequestIntent().getBody(); assertThat(body.getString()).contains("sort=variants.availability.channels.channel-id-500.restockableInDays+asc"); }