Home | History | Annotate | Download | only in okhttp
      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.ByteArrayOutputStream;
     20 import java.io.File;
     21 import java.io.FileWriter;
     22 import java.io.IOException;
     23 import org.junit.Test;
     24 
     25 import static org.junit.Assert.assertEquals;
     26 
     27 public final class RequestTest {
     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 
     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 
     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 
     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 
     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 
     76   private String bodyToHex(Request.Body body) throws IOException {
     77     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     78     body.writeTo(bytes);
     79     return bytesToHex(bytes.toByteArray());
     80   }
     81 
     82   private String bytesToHex(byte[] bytes) {
     83     StringBuilder hex = new StringBuilder();
     84     for (byte b : bytes) {
     85       if ((b & 0xff) < 0x10) hex.append('0');
     86       hex.append(Integer.toHexString(b & 0xff));
     87     }
     88     return hex.toString();
     89   }
     90 }
     91