1 /* 2 * Copyright (C) 2013 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp; 17 18 import com.squareup.okhttp.internal.Util; 19 import java.io.File; 20 import java.io.FileWriter; 21 import java.io.IOException; 22 import java.net.URI; 23 import java.net.URL; 24 import java.util.Arrays; 25 import java.util.Collections; 26 import okio.Buffer; 27 import org.junit.Test; 28 29 import static org.junit.Assert.assertEquals; 30 import static org.junit.Assert.assertNull; 31 32 public final class RequestTest { 33 @Test public void string() throws Exception { 34 MediaType contentType = MediaType.parse("text/plain; charset=utf-8"); 35 RequestBody body = RequestBody.create(contentType, "abc".getBytes(Util.UTF_8)); 36 assertEquals(contentType, body.contentType()); 37 assertEquals(3, body.contentLength()); 38 assertEquals("616263", bodyToHex(body)); 39 assertEquals("Retransmit body", "616263", bodyToHex(body)); 40 } 41 42 @Test public void stringWithDefaultCharsetAdded() throws Exception { 43 MediaType contentType = MediaType.parse("text/plain"); 44 RequestBody body = RequestBody.create(contentType, "\u0800"); 45 assertEquals(MediaType.parse("text/plain; charset=utf-8"), body.contentType()); 46 assertEquals(3, body.contentLength()); 47 assertEquals("e0a080", bodyToHex(body)); 48 } 49 50 @Test public void stringWithNonDefaultCharsetSpecified() throws Exception { 51 MediaType contentType = MediaType.parse("text/plain; charset=utf-16be"); 52 RequestBody body = RequestBody.create(contentType, "\u0800"); 53 assertEquals(contentType, body.contentType()); 54 assertEquals(2, body.contentLength()); 55 assertEquals("0800", bodyToHex(body)); 56 } 57 58 @Test public void byteArray() throws Exception { 59 MediaType contentType = MediaType.parse("text/plain"); 60 RequestBody body = RequestBody.create(contentType, "abc".getBytes(Util.UTF_8)); 61 assertEquals(contentType, body.contentType()); 62 assertEquals(3, body.contentLength()); 63 assertEquals("616263", bodyToHex(body)); 64 assertEquals("Retransmit body", "616263", bodyToHex(body)); 65 } 66 67 @Test public void byteArrayRange() throws Exception { 68 MediaType contentType = MediaType.parse("text/plain"); 69 RequestBody body = RequestBody.create(contentType, ".abcd".getBytes(Util.UTF_8), 1, 3); 70 assertEquals(contentType, body.contentType()); 71 assertEquals(3, body.contentLength()); 72 assertEquals("616263", bodyToHex(body)); 73 assertEquals("Retransmit body", "616263", bodyToHex(body)); 74 } 75 76 @Test public void file() throws Exception { 77 File file = File.createTempFile("RequestTest", "tmp"); 78 FileWriter writer = new FileWriter(file); 79 writer.write("abc"); 80 writer.close(); 81 82 MediaType contentType = MediaType.parse("text/plain"); 83 RequestBody body = RequestBody.create(contentType, file); 84 assertEquals(contentType, body.contentType()); 85 assertEquals(3, body.contentLength()); 86 assertEquals("616263", bodyToHex(body)); 87 assertEquals("Retransmit body", "616263", bodyToHex(body)); 88 } 89 90 /** Common verbs used for apis such as GitHub, AWS, and Google Cloud. */ 91 @Test public void crudVerbs() throws IOException { 92 MediaType contentType = MediaType.parse("application/json"); 93 RequestBody body = RequestBody.create(contentType, "{}"); 94 95 Request get = new Request.Builder().url("http://localhost/api").get().build(); 96 assertEquals("GET", get.method()); 97 assertNull(get.body()); 98 99 Request head = new Request.Builder().url("http://localhost/api").head().build(); 100 assertEquals("HEAD", head.method()); 101 assertNull(head.body()); 102 103 Request delete = new Request.Builder().url("http://localhost/api").delete().build(); 104 assertEquals("DELETE", delete.method()); 105 assertEquals(0L, delete.body().contentLength()); 106 107 Request post = new Request.Builder().url("http://localhost/api").post(body).build(); 108 assertEquals("POST", post.method()); 109 assertEquals(body, post.body()); 110 111 Request put = new Request.Builder().url("http://localhost/api").put(body).build(); 112 assertEquals("PUT", put.method()); 113 assertEquals(body, put.body()); 114 115 Request patch = new Request.Builder().url("http://localhost/api").patch(body).build(); 116 assertEquals("PATCH", patch.method()); 117 assertEquals(body, patch.body()); 118 } 119 120 @Test public void uninitializedURI() throws Exception { 121 Request request = new Request.Builder().url("http://localhost/api").build(); 122 assertEquals(new URI("http://localhost/api"), request.uri()); 123 assertEquals(new URL("http://localhost/api"), request.url()); 124 } 125 126 @Test public void newBuilderUrlResetsUrl() throws Exception { 127 Request requestWithoutCache = new Request.Builder().url("http://localhost/api").build(); 128 Request builtRequestWithoutCache = requestWithoutCache.newBuilder().url("http://localhost/api/foo").build(); 129 assertEquals(new URL("http://localhost/api/foo"), builtRequestWithoutCache.url()); 130 131 Request requestWithCache = new Request.Builder().url("http://localhost/api").build(); 132 // cache url object 133 requestWithCache.url(); 134 Request builtRequestWithCache = requestWithCache.newBuilder().url("http://localhost/api/foo").build(); 135 assertEquals(new URL("http://localhost/api/foo"), builtRequestWithCache.url()); 136 } 137 138 @Test public void cacheControl() throws Exception { 139 Request request = new Request.Builder() 140 .cacheControl(new CacheControl.Builder().noCache().build()) 141 .url("https://square.com") 142 .build(); 143 assertEquals(Arrays.asList("no-cache"), request.headers("Cache-Control")); 144 } 145 146 @Test public void emptyCacheControlClearsAllCacheControlHeaders() throws Exception { 147 Request request = new Request.Builder() 148 .header("Cache-Control", "foo") 149 .cacheControl(new CacheControl.Builder().build()) 150 .url("https://square.com") 151 .build(); 152 assertEquals(Collections.<String>emptyList(), request.headers("Cache-Control")); 153 } 154 155 private String bodyToHex(RequestBody body) throws IOException { 156 Buffer buffer = new Buffer(); 157 body.writeTo(buffer); 158 return buffer.readByteString().hex(); 159 } 160 } 161