Skip to content

Commit 4658d45

Browse files
author
Shashi Ranjan
committed
okhttp snippet/test
1 parent b253812 commit 4658d45

16 files changed

Lines changed: 156 additions & 15 deletions

src/targets/java/okhttp.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @description
3-
* HTTP code snippet generator for Java using Unirest.
3+
* HTTP code snippet generator for Java using OkHttp.
44
*
55
* @author
66
* @shashiranjan84
@@ -21,24 +21,31 @@ module.exports = function (source, options) {
2121
var code = new CodeBuilder(opts.indent)
2222

2323
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
24-
24+
2525
if (methods.indexOf(source.method.toUpperCase()) === -1) {
26-
return 'Method not supported'
26+
return 'Method not supported'
2727
}
28-
28+
29+
code.push('OkHttpClient client = new OkHttpClient();')
30+
.blank()
31+
2932
if (source.postData.text) {
30-
RequestBody body = RequestBody.create(JSON, json);
31-
code.push(1, 'RequestBody body = RequestBody.create("%s", %s);', source.postData.mimeType, JSON.stringify(source.postData.text))
33+
if (source.postData.boundary) {
34+
code.push('MediaType mediaType = MediaType.parse("%s; boundary=%s");', source.postData.mimeType, source.postData.boundary)
35+
}else {
36+
code.push('MediaType mediaType = MediaType.parse("%s");', source.postData.mimeType)
37+
}
38+
code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
3239
}
33-
40+
3441
code.push('Request request = new Request.Builder()')
35-
code.push('.url("%s")', source.fullUrl)
36-
if(source.postData.text){
37-
code.push(1,'.%s(body)', source.method)
38-
}else{
39-
code.push(1,'.%s()', source.method)
42+
code.push(1, '.url("%s")', source.fullUrl)
43+
if (source.postData.text) {
44+
code.push(1, '.%s(body)', source.method.toLowerCase())
45+
}else {
46+
code.push(1, '.%s()', source.method.toLowerCase())
4047
}
41-
48+
4249
// Add headers, including the cookies
4350
var headers = Object.keys(source.allHeaders)
4451

@@ -50,8 +57,8 @@ module.exports = function (source, options) {
5057
}
5158

5259
code.push(1, '.build();')
53-
54-
code.push('Response response = client.newCall(request).execute();')
60+
.blank()
61+
.push('Response response = client.newCall(request).execute();')
5562

5663
return code.join()
5764
}

test/fixtures/available-targets.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@
179179
"extname": ".java",
180180
"default": "unirest",
181181
"clients": [
182+
{
183+
"key": "okhttp",
184+
"title": "OkHttp",
185+
"link": "http://square.github.io/okhttp/",
186+
"description": "An HTTP Request Client Library"
187+
},
182188
{
183189
"key": "unirest",
184190
"title": "Unirest",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
4+
RequestBody body = RequestBody.create(mediaType, "foo=bar&hello=world");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/x-www-form-urlencoded")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/json");
4+
RequestBody body = RequestBody.create(mediaType, "{\"number\":1,\"string\":\"f\\\"oo\",\"arr\":[1,2,3],\"nested\":{\"a\":\"b\"},\"arr_mix\":[1,\"a\",{\"arr_mix_nested\":{}}]}");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "application/json")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.post()
6+
.addHeader("cookie", "foo=bar; bar=baz")
7+
.build();
8+
9+
Response response = client.newCall(request).execute();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Method not supported
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
4+
RequestBody body = RequestBody.create(mediaType, "foo=bar");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value")
7+
.post(body)
8+
.addHeader("cookie", "foo=bar; bar=baz")
9+
.addHeader("accept", "application/json")
10+
.addHeader("content-type", "application/x-www-form-urlencoded")
11+
.build();
12+
13+
Response response = client.newCall(request).execute();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("http://mockbin.com/har")
5+
.get()
6+
.addHeader("accept", "application/json")
7+
.addHeader("x-foo", "Bar")
8+
.build();
9+
10+
Response response = client.newCall(request).execute();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
Request request = new Request.Builder()
4+
.url("https://mockbin.com/har")
5+
.get()
6+
.build();
7+
8+
Response response = client.newCall(request).execute();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OkHttpClient client = new OkHttpClient();
2+
3+
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
4+
RequestBody body = RequestBody.create(mediaType, "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"foo\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nHello World\r\n-----011000010111000001101001--");
5+
Request request = new Request.Builder()
6+
.url("http://mockbin.com/har")
7+
.post(body)
8+
.addHeader("content-type", "multipart/form-data; boundary=---011000010111000001101001")
9+
.build();
10+
11+
Response response = client.newCall(request).execute();

0 commit comments

Comments
 (0)