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