1/*
2 * Copyright (C) 2014 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 */
16package com.squareup.okhttp.curl;
17
18import com.squareup.okhttp.Request;
19import java.io.IOException;
20import okio.OkBuffer;
21import org.junit.Test;
22
23import static com.squareup.okhttp.curl.Main.fromArgs;
24import static org.junit.Assert.assertEquals;
25import static org.junit.Assert.assertNull;
26
27public class MainTest {
28  @Test public void simple() {
29    Request request = fromArgs("http://example.com").createRequest();
30    assertEquals("GET", request.method());
31    assertEquals("http://example.com", request.urlString());
32    assertNull(request.body());
33  }
34
35  @Test public void put() {
36    Request request = fromArgs("-X", "PUT", "http://example.com").createRequest();
37    assertEquals("PUT", request.method());
38    assertEquals("http://example.com", request.urlString());
39    assertNull(request.body());
40  }
41
42  @Test public void dataPost() {
43    Request request = fromArgs("-d", "foo", "http://example.com").createRequest();
44    Request.Body body = request.body();
45    assertEquals("POST", request.method());
46    assertEquals("http://example.com", request.urlString());
47    assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
48    assertEquals("foo", bodyAsString(body));
49  }
50
51  @Test public void dataPut() {
52    Request request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest();
53    Request.Body body = request.body();
54    assertEquals("PUT", request.method());
55    assertEquals("http://example.com", request.urlString());
56    assertEquals("application/x-form-urlencoded; charset=utf-8", body.contentType().toString());
57    assertEquals("foo", bodyAsString(body));
58  }
59
60  @Test public void contentTypeHeader() {
61    Request request = fromArgs("-d", "foo", "-H", "Content-Type: application/json",
62        "http://example.com").createRequest();
63    Request.Body body = request.body();
64    assertEquals("POST", request.method());
65    assertEquals("http://example.com", request.urlString());
66    assertEquals("application/json; charset=utf-8", body.contentType().toString());
67    assertEquals("foo", bodyAsString(body));
68  }
69
70  @Test public void referer() {
71    Request request = fromArgs("-e", "foo", "http://example.com").createRequest();
72    assertEquals("GET", request.method());
73    assertEquals("http://example.com", request.urlString());
74    assertEquals("foo", request.header("Referer"));
75    assertNull(request.body());
76  }
77
78  @Test public void userAgent() {
79    Request request = fromArgs("-A", "foo", "http://example.com").createRequest();
80    assertEquals("GET", request.method());
81    assertEquals("http://example.com", request.urlString());
82    assertEquals("foo", request.header("User-Agent"));
83    assertNull(request.body());
84  }
85
86  private static String bodyAsString(Request.Body body) {
87    try {
88      OkBuffer buffer = new OkBuffer();
89      body.writeTo(buffer);
90      return new String(buffer.readByteString(buffer.size()).toByteArray(),
91          body.contentType().charset());
92    } catch (IOException e) {
93      throw new RuntimeException(e);
94    }
95  }
96}
97