Skip to content

Commit 22734da

Browse files
committed
Merge pull request kittinunf#89 from kittinunf/head_support
head support
2 parents f395204 + a4b914d commit 22734da

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The easiest HTTP networking library for Kotlin/Android.
66

77
## Features
88

9-
- [x] Support basic HTTP GET/POST/PUT/DELETE in a fluent style interface
9+
- [x] Support basic HTTP GET/POST/PUT/DELETE/HEAD in a fluent style interface
1010
- [x] Support both asynchronous and blocking requests
1111
- [x] Download file
1212
- [x] Upload file (multipart/form-data)
@@ -195,6 +195,14 @@ Fuel.delete("http://httpbin.org/delete").response { request, response, result ->
195195
}
196196
```
197197

198+
### HEAD
199+
200+
``` Kotlin
201+
Fuel.head("http://httpbin.org/get").response { request, response, result ->
202+
// request body should be empty.
203+
}
204+
```
205+
198206
### Debug Logging
199207
* Use `toString()` method to Log (request|response)
200208

fuel/src/main/kotlin/com/github/kittinunf/fuel/Fuel.kt

+21
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ class Fuel {
9292
return upload(convertible.path, method, parameters)
9393
}
9494

95+
//head
96+
@JvmStatic @JvmOverloads
97+
fun head(path: String, parameters: List<Pair<String, Any?>>? = null): Request {
98+
return request(Method.HEAD, path, parameters)
99+
}
100+
101+
@JvmStatic @JvmOverloads
102+
fun head(convertible: PathStringConvertible, parameters: List<Pair<String, Any?>>? = null): Request {
103+
return request(Method.HEAD, convertible, parameters)
104+
}
105+
95106
//request
96107
private fun request(method: Method, path: String, parameters: List<Pair<String, Any?>>? = null): Request {
97108
return FuelManager.instance.request(method, path, parameters)
@@ -169,3 +180,13 @@ fun String.httpUpload(method: Method = Method.POST, parameters: List<Pair<String
169180
fun Fuel.PathStringConvertible.httpUpload(method: Method = Method.POST, parameters: List<Pair<String, Any?>>? = null): Request {
170181
return Fuel.upload(this, method, parameters)
171182
}
183+
184+
@JvmOverloads
185+
fun Fuel.PathStringConvertible.httpHead(parameter: List<Pair<String, Any?>>? = null): Request {
186+
return Fuel.head(this, parameter)
187+
}
188+
189+
@JvmOverloads
190+
fun String.httpHead(parameters: List<Pair<String, Any?>>? = null): Request {
191+
return Fuel.head(this, parameters)
192+
}

fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Encoding.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Encoding : Fuel.RequestConvertible {
6767

6868
private fun encodeParameterInUrl(method: Method): Boolean {
6969
when (method) {
70-
Method.GET, Method.DELETE -> return true
70+
Method.GET, Method.DELETE, Method.HEAD -> return true
7171
else -> return false
7272
}
7373
}

fuel/src/main/kotlin/com/github/kittinunf/fuel/core/Method.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum class Method(val value: String) {
55
POST("POST"),
66
PUT("PUT"),
77
DELETE("DELETE"),
8-
PATCH("PATCH")
8+
PATCH("PATCH"),
9+
HEAD("HEAD")
910
}
1011

fuel/src/main/kotlin/com/github/kittinunf/fuel/toolbox/HttpClient.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class HttpClient : Client {
9898

9999
private fun setDoOutput(connection: HttpURLConnection, method: Method) {
100100
when (method) {
101-
Method.GET, Method.DELETE -> connection.doOutput = false
101+
Method.GET, Method.DELETE, Method.HEAD -> connection.doOutput = false
102102
Method.POST, Method.PUT, Method.PATCH -> connection.doOutput = true
103103
}
104104
}

fuel/src/test/kotlin/com/github/kittinunf/fuel/RequestTest.kt

+32
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,38 @@ class RequestTest : BaseTestCase() {
249249
assertThat(string, containsString(paramValue))
250250
}
251251

252+
@Test
253+
fun httpHeadRequest() {
254+
var request: Request? = null
255+
var response: Response? = null
256+
var data: Any? = null
257+
var error: FuelError? = null
258+
259+
val paramKey = "foo"
260+
val paramValue = "bar"
261+
262+
manager.request(Method.HEAD, "http://httpbin.org/get", listOf(paramKey to paramValue)).responseString { req, res, result ->
263+
request = req
264+
response = res
265+
266+
val (d, err) = result
267+
data = d
268+
error = err
269+
}
270+
271+
val string = data as String
272+
273+
assertThat(request, notNullValue())
274+
assertThat(response, notNullValue())
275+
assertThat(error, nullValue())
276+
assertThat(data, notNullValue())
277+
278+
val statusCode = HttpURLConnection.HTTP_OK
279+
assertThat(response?.httpStatusCode, isEqualTo(statusCode))
280+
281+
assertThat(string, equalTo(""))
282+
}
283+
252284
@Test
253285
fun httpGetRequestWithPathStringConvertible() {
254286
var request: Request? = null

0 commit comments

Comments
 (0)