-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
Copy pathMainTest.kt
151 lines (138 loc) · 4.55 KB
/
MainTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.curl
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNull
import assertk.assertions.startsWith
import com.github.ajalt.clikt.core.parse
import java.io.IOException
import kotlin.test.Test
import okhttp3.RequestBody
import okhttp3.TestUtil
import okio.Buffer
import org.junit.jupiter.api.BeforeAll
class MainTest {
@Test
fun simple() {
val request = fromArgs("http://example.com").createRequest()
assertThat(request.method).isEqualTo("GET")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(request.body).isNull()
}
@Test
@Throws(IOException::class)
fun put() {
val request = fromArgs("-X", "PUT", "-d", "foo", "http://example.com").createRequest()
assertThat(request.method).isEqualTo("PUT")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(request.body!!.contentLength()).isEqualTo(3)
}
@Test
fun dataPost() {
val request = fromArgs("-d", "foo", "http://example.com").createRequest()
val body = request.body
assertThat(request.method).isEqualTo("POST")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(body!!.contentType().toString()).isEqualTo(
"application/x-www-form-urlencoded; charset=utf-8",
)
assertThat(bodyAsString(body)).isEqualTo("foo")
}
@Test
fun dataPut() {
val request = fromArgs("-d", "foo", "-X", "PUT", "http://example.com").createRequest()
val body = request.body
assertThat(request.method).isEqualTo("PUT")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(body!!.contentType().toString()).isEqualTo(
"application/x-www-form-urlencoded; charset=utf-8",
)
assertThat(bodyAsString(body)).isEqualTo("foo")
}
@Test
fun contentTypeHeader() {
val request =
fromArgs(
"-d",
"foo",
"-H",
"Content-Type: application/json",
"http://example.com",
).createRequest()
val body = request.body
assertThat(request.method).isEqualTo("POST")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(body!!.contentType().toString())
.isEqualTo("application/json; charset=utf-8")
assertThat(bodyAsString(body)).isEqualTo("foo")
}
@Test
fun referer() {
val request = fromArgs("-e", "foo", "http://example.com").createRequest()
assertThat(request.method).isEqualTo("GET")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(request.header("Referer")).isEqualTo("foo")
assertThat(request.body).isNull()
}
@Test
fun userAgent() {
val request = fromArgs("-A", "foo", "http://example.com").createRequest()
assertThat(request.method).isEqualTo("GET")
assertThat(request.url.toString()).isEqualTo("http://example.com/")
assertThat(request.header("User-Agent")).isEqualTo("foo")
assertThat(request.body).isNull()
}
@Test
fun defaultUserAgent() {
val request = fromArgs("http://example.com").createRequest()
assertThat(request.header("User-Agent")!!).startsWith("okcurl/")
}
@Test
fun headerSplitWithDate() {
val request =
fromArgs(
"-H",
"If-Modified-Since: Mon, 18 Aug 2014 15:16:06 GMT",
"http://example.com",
).createRequest()
assertThat(request.header("If-Modified-Since")).isEqualTo(
"Mon, 18 Aug 2014 15:16:06 GMT",
)
}
companion object {
fun fromArgs(vararg args: String): Main {
return Main().apply {
parse(args.toList())
}
}
private fun bodyAsString(body: RequestBody?): String {
return try {
val buffer = Buffer()
body!!.writeTo(buffer)
buffer.readString(body.contentType()!!.charset()!!)
} catch (e: IOException) {
throw RuntimeException(e)
}
}
@JvmStatic
@BeforeAll
fun beforeAll() {
// https://github.com/square/okhttp/issues/8342
TestUtil.assumeNotWindows()
}
}
}