@Override public void addHeader(String key, String value) { request.addHeader(new BasicHeader(key, value)); }
@Override public void addHeader(String key, String value) { request.addHeader(new BasicHeader(key, value)); }
@Override @Nullable public Header getContentEncoding() { String contentEncoding = this.headers.getFirst("Content-Encoding"); return (contentEncoding != null ? new BasicHeader("Content-Encoding", contentEncoding) : null); }
/** * Set the default header collection (creating copies of the provided headers). */ public void setDefaultHeaders(Collection<? extends Header> defaultHeaders) { Collection<BasicHeader> copiedHeaders = new HashSet<>(); for (Header header : defaultHeaders) { copiedHeaders.add(new BasicHeader(header.getName(), header.getValue())); } this.defaultHeaders = copiedHeaders; }
@Override @Nullable public Header getContentType() { MediaType contentType = this.headers.getContentType(); return (contentType != null ? new BasicHeader("Content-Type", contentType.toString()) : null); }
@Override @Nullable public Header getContentEncoding() { String contentEncoding = this.headers.getFirst("Content-Encoding"); return (contentEncoding != null ? new BasicHeader("Content-Encoding", contentEncoding) : null); }
private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) { this.portalUrl = portalUrl; this.token = token; CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setDefaultHeaders(Lists.newArrayList(new BasicHeader("Authorization", token))).build(); Gson gson = new GsonBuilder().setDateFormat(ApolloOpenApiConstants.JSON_DATE_FORMAT).create(); String baseUrl = this.portalUrl + ApolloOpenApiConstants.OPEN_API_V1_PREFIX; appService = new AppOpenApiService(client, baseUrl, gson); namespaceService = new NamespaceOpenApiService(client, baseUrl, gson); itemService = new ItemOpenApiService(client, baseUrl, gson); releaseService = new ReleaseOpenApiService(client, baseUrl, gson); }
@Override @Nullable public Header getContentType() { MediaType contentType = this.headers.getContentType(); return (contentType != null ? new BasicHeader("Content-Type", contentType.toString()) : null); }
private List<Cookie> extractResponseCookies(Response response, CookieOrigin cookieOrigin) { List<Cookie> cookies = new ArrayList<Cookie>(); for (String cookieValue : response.getHeaders().getValues("Set-Cookie")) { Header setCookieHeader = new BasicHeader("Set-Cookie", cookieValue); try { cookies.addAll(cookieSpec.parse(setCookieHeader, cookieOrigin)); } catch (MalformedCookieException ignored) { } } return cookies; }
@Test public void shouldSupportGetHeadersWithArg() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("X-Zombo-Com", "http://zombo.com"), new BasicHeader("Location", "http://foo.com")); Header[] headers = resp.getHeaders("Location"); assertThat(headers.length).isEqualTo(2); assertThat(headers[0].getValue()).isEqualTo("http://bar.com"); assertThat(headers[1].getValue()).isEqualTo("http://foo.com"); }
@Test public void shouldSupportGetLastHeader() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("Location", "http://zombo.com")); assertThat(resp.getLastHeader("None")).isNull(); for (String l : new String[] { "location", "Location" }) { assertThat(resp.getLastHeader(l).getValue()).isEqualTo("http://zombo.com"); } }
@Test public void onlyOverridesFirstHeaderValue() { TestHttpResponse response = new TestHttpResponse(200, "abc", new BasicHeader("foo", "bar"), new BasicHeader("foo", "baz")); response.setHeader(new BasicHeader("foo", "bletch")); assertThat(response.getAllHeaders().length).isEqualTo(2); assertThat(response.getHeaders("foo")[0].getValue()).isEqualTo("bletch"); assertThat(response.getHeaders("foo")[1].getValue()).isEqualTo("baz"); }
private void assertExtraPropertiesWithoutBase64(String actualHeaderValue, Map<String, String> expectedProperties) { Map<String, String> actualResult = HeaderUtil.parseExtraProperties(new BasicHeader("some-key", actualHeaderValue)); assertThat(actualResult, is(expectedProperties)); }
@Test public void canOverrideExistingHeaderValue() { TestHttpResponse response = new TestHttpResponse(200, "abc", new BasicHeader("foo", "bar")); response.setHeader(new BasicHeader("foo", "bletch")); assertThat(response.getAllHeaders().length).isEqualTo(1); assertThat(response.getHeaders("foo")[0].getValue()).isEqualTo("bletch"); }
@Test public void matches_shouldMatchHeaders() throws Exception { requestMatcherBuilder.header("header1", "header one"); HttpGet match = new HttpGet("example.com"); HttpGet noMatch = new HttpGet("example.com"); match.setHeader(new BasicHeader("header1", "header one")); noMatch.setHeader(new BasicHeader("header1", "header not a match")); assertThat(requestMatcherBuilder.matches(new HttpGet("example.com"))).isFalse(); assertThat(requestMatcherBuilder.matches(noMatch)).isFalse(); assertThat(requestMatcherBuilder.matches(match)).isTrue(); }
@Test public void shouldSupportHeaderIterator() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com"), new BasicHeader("Location", "http://zombo.com")); HeaderIterator it = resp.headerIterator(); assertThat(it.hasNext()).isTrue(); assertThat(it.nextHeader().getValue()).isEqualTo("http://bar.com"); assertThat(it.nextHeader().getValue()).isEqualTo("http://zombo.com"); assertThat(it.hasNext()).isFalse(); }
@Test public void shouldSupportGetFirstHeader() throws Exception { HttpResponse resp = new TestHttpResponse(304, "REDIRECTED", new BasicHeader("Location", "http://bar.com")); assertThat(resp.getFirstHeader("None")).isNull(); assertThat(new TestHttpResponse(200, "OK").getFirstHeader("Foo")).isNull(); for (String l : new String[] { "location", "Location" }) { assertThat(resp.getFirstHeader(l).getValue()).isEqualTo("http://bar.com"); } }
@Test public void shouldSupportContainsHeader() throws Exception { HttpResponse resp = new TestHttpResponse(304, "ZOMBO", new BasicHeader("X-Zombo-Com", "Welcome")); assertThat(resp.containsHeader("X-Zombo-Com")).isTrue(); assertThat(resp.containsHeader("Location")).isFalse(); }
@Test public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception { FakeHttp.addPendingHttpResponse(200, "OK", new BasicHeader("Content-Type", "text/plain")); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(new HttpGet("http://www.nowhere.org")); assertThat(((HttpUriRequest) FakeHttp.getSentHttpRequest(0)).getURI()) .isEqualTo(URI.create("http://www.nowhere.org")); Assert.assertNotNull(response); String responseStr = new BasicResponseHandler().handleResponse(response); Assert.assertEquals("OK", responseStr); }
@Test public void canAddNewBasicHeader() { TestHttpResponse response = new TestHttpResponse(200, "abc"); assertThat(response.getAllHeaders().length).isEqualTo(0); response.addHeader(new BasicHeader("foo", "bar")); assertThat(response.getAllHeaders().length).isEqualTo(1); assertThat(response.getHeaders("foo")[0].getValue()).isEqualTo("bar"); }