use of okhttp3.Headers in project retrofit by square.
the class RequestBuilderTest method headerParam.
@Test
public void headerParam() {
class Example {
//
@GET("/foo/bar/")
//
@Headers("ping: pong")
Call<ResponseBody> method(@Header("kit") String kit) {
return null;
}
}
Request request = buildRequest(Example.class, "kat");
assertThat(request.method()).isEqualTo("GET");
okhttp3.Headers headers = request.headers();
assertThat(headers.size()).isEqualTo(2);
assertThat(headers.get("ping")).isEqualTo("pong");
assertThat(headers.get("kit")).isEqualTo("kat");
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
use of okhttp3.Headers in project retrofit by square.
the class RequestBuilderTest method getWithHeaderMap.
@Test
public void getWithHeaderMap() {
class Example {
@GET("/search")
Call<ResponseBody> method(@HeaderMap Map<String, Object> headers) {
return null;
}
}
Map<String, Object> headers = new LinkedHashMap<>();
headers.put("Accept", "text/plain");
headers.put("Accept-Charset", "utf-8");
Request request = buildRequest(Example.class, headers);
assertThat(request.method()).isEqualTo("GET");
assertThat(request.url().toString()).isEqualTo("http://example.com/search");
assertThat(request.body()).isNull();
assertThat(request.headers().size()).isEqualTo(2);
assertThat(request.header("Accept")).isEqualTo("text/plain");
assertThat(request.header("Accept-Charset")).isEqualTo("utf-8");
}
use of okhttp3.Headers in project retrofit by square.
the class RequestBuilderTest method malformedContentTypeHeaderThrows.
@Test
public void malformedContentTypeHeaderThrows() {
class Example {
//
@POST("/")
//
@Headers("Content-Type: hello, world!")
Call<ResponseBody> method(@Body RequestBody body) {
return null;
}
}
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
try {
buildRequest(Example.class, body);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Malformed content type: hello, world!\n" + " for method Example.method");
}
}
use of okhttp3.Headers in project okhttp by square.
the class MockWebServer method handleWebSocketUpgrade.
private void handleWebSocketUpgrade(Socket socket, BufferedSource source, BufferedSink sink, RecordedRequest request, MockResponse response) throws IOException {
String key = request.getHeader("Sec-WebSocket-Key");
response.setHeader("Sec-WebSocket-Accept", WebSocketProtocol.acceptHeader(key));
writeHttpResponse(socket, sink, response);
// Adapt the request and response into our Request and Response domain model.
String scheme = request.getTlsVersion() != null ? "https" : "http";
// Has host and port.
String authority = request.getHeader("Host");
final Request fancyRequest = new Request.Builder().url(scheme + "://" + authority + "/").headers(request.getHeaders()).build();
final Response fancyResponse = new Response.Builder().code(Integer.parseInt(response.getStatus().split(" ")[1])).message(response.getStatus().split(" ", 3)[2]).headers(response.getHeaders()).request(fancyRequest).protocol(Protocol.HTTP_1_1).build();
final CountDownLatch connectionClose = new CountDownLatch(1);
RealWebSocket.Streams streams = new RealWebSocket.Streams(false, source, sink) {
@Override
public void close() {
connectionClose.countDown();
}
};
RealWebSocket webSocket = new RealWebSocket(fancyRequest, response.getWebSocketListener(), new SecureRandom());
response.getWebSocketListener().onOpen(webSocket, fancyResponse);
String name = "MockWebServer WebSocket " + request.getPath();
webSocket.initReaderAndWriter(name, 0, streams);
try {
webSocket.loopReader();
// Even if messages are no longer being read we need to wait for the connection close signal.
try {
connectionClose.await();
} catch (InterruptedException ignored) {
}
} catch (IOException e) {
webSocket.failWebSocket(e, null);
} finally {
closeQuietly(sink);
closeQuietly(source);
}
}
use of okhttp3.Headers in project okhttp by square.
the class CacheAdapterTest method get_httpGet.
@Test
public void get_httpGet() throws Exception {
final URL serverUrl = configureServer(new MockResponse());
assertEquals("http", serverUrl.getProtocol());
ResponseCache responseCache = new AbstractResponseCache() {
@Override
public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
try {
assertEquals(toUri(serverUrl), uri);
assertEquals("GET", method);
assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
assertEquals(Collections.singletonList("value1"), headers.get("key1"));
return null;
} catch (Throwable t) {
throw new IOException("unexpected cache failure", t);
}
}
};
setInternalCache(new CacheAdapter(responseCache));
connection = new OkUrlFactory(client).open(serverUrl);
connection.setRequestProperty("key1", "value1");
executeGet(connection);
}
Aggregations