• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 okio.OkBuffer;
23 import org.junit.Test;
24 
25 import static org.junit.Assert.assertEquals;
26 
27 public final class RequestTest {
string()28   @Test public void string() throws Exception {
29     MediaType contentType = MediaType.parse("text/plain; charset=utf-8");
30     Request.Body body = Request.Body.create(contentType, "abc".getBytes(Util.UTF_8));
31     assertEquals(contentType, body.contentType());
32     assertEquals(3, body.contentLength());
33     assertEquals("616263", bodyToHex(body));
34     assertEquals("Retransmit body", "616263", bodyToHex(body));
35   }
36 
stringWithDefaultCharsetAdded()37   @Test public void stringWithDefaultCharsetAdded() throws Exception {
38     MediaType contentType = MediaType.parse("text/plain");
39     Request.Body body = Request.Body.create(contentType, "\u0800");
40     assertEquals(MediaType.parse("text/plain; charset=utf-8"), body.contentType());
41     assertEquals(3, body.contentLength());
42     assertEquals("e0a080", bodyToHex(body));
43   }
44 
stringWithNonDefaultCharsetSpecified()45   @Test public void stringWithNonDefaultCharsetSpecified() throws Exception {
46     MediaType contentType = MediaType.parse("text/plain; charset=utf-16be");
47     Request.Body body = Request.Body.create(contentType, "\u0800");
48     assertEquals(contentType, body.contentType());
49     assertEquals(2, body.contentLength());
50     assertEquals("0800", bodyToHex(body));
51   }
52 
byteArray()53   @Test public void byteArray() throws Exception {
54     MediaType contentType = MediaType.parse("text/plain");
55     Request.Body body = Request.Body.create(contentType, "abc".getBytes(Util.UTF_8));
56     assertEquals(contentType, body.contentType());
57     assertEquals(3, body.contentLength());
58     assertEquals("616263", bodyToHex(body));
59     assertEquals("Retransmit body", "616263", bodyToHex(body));
60   }
61 
file()62   @Test public void file() throws Exception {
63     File file = File.createTempFile("RequestTest", "tmp");
64     FileWriter writer = new FileWriter(file);
65     writer.write("abc");
66     writer.close();
67 
68     MediaType contentType = MediaType.parse("text/plain");
69     Request.Body body = Request.Body.create(contentType, file);
70     assertEquals(contentType, body.contentType());
71     assertEquals(3, body.contentLength());
72     assertEquals("616263", bodyToHex(body));
73     assertEquals("Retransmit body", "616263", bodyToHex(body));
74   }
75 
bodyToHex(Request.Body body)76   private String bodyToHex(Request.Body body) throws IOException {
77     OkBuffer buffer = new OkBuffer();
78     body.writeTo(buffer);
79     return buffer.readByteString(buffer.size()).hex();
80   }
81 }
82