Skip to content

Commit 84e2195

Browse files
committed
Merge branch 'montanaflynn-master'
2 parents 9f36b09 + 9c24edd commit 84e2195

File tree

17 files changed

+340
-0
lines changed

17 files changed

+340
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#requ
2525
currently the following output [targets](/src/targets) are supported:
2626

2727
- [cURL](http://curl.haxx.se/)
28+
- [Go](http://golang.org/pkg/net/http/#NewRequest)
2829
- [HTTPie](http://httpie.org)
2930
- Node.js
3031
- [Native](http://nodejs.org/api/http.html#http_http_request_options_callback)
@@ -185,6 +186,13 @@ module.exports.info = {
185186
| `short` | `false` | use short form of cURL CLI options |
186187
| `indent` | ` ` | line break & indent output value, set to `false` to disable line breaks |
187188

189+
### Go
190+
191+
| Option | Default | Description |
192+
| --------------- | ------- | ------------------------------------------------------------------------ |
193+
| `errorChecking` | `false` | add error checking for request, response and body |
194+
| `printBody` | `true` | include code to print the body as a string |
195+
188196
### HTTPie
189197

190198
| Option | Default | Description |

src/targets/go/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('require-directory')(module);

src/targets/go/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
key: 'go',
5+
title: 'Go',
6+
extname: '.go',
7+
default: 'native'
8+
};

src/targets/go/native.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function (source, options) {
6+
// Let's Go!
7+
var code = [];
8+
9+
// Define Options
10+
var opts = util._extend({
11+
checkErrors: false,
12+
printBody: true
13+
}, options);
14+
15+
var errorPlaceholder = opts.checkErrors ? 'err' : '_';
16+
17+
var errorCheck = function () {
18+
if (opts.checkErrors) {
19+
code.push('\tif err != nil {');
20+
code.push('\t\tpanic(err)');
21+
code.push('\t}');
22+
}
23+
};
24+
25+
// Create boilerplate
26+
code.push('package main\n');
27+
code.push('import (');
28+
code.push('\t"fmt"');
29+
30+
if (source.postData.text) {
31+
code.push('\t"strings"');
32+
}
33+
34+
code.push('\t"net/http"');
35+
36+
if (opts.printBody) {
37+
code.push('\t"io/ioutil"');
38+
}
39+
code.push(')\n');
40+
41+
code.push('func main() {');
42+
43+
// Create client
44+
code.push('\tclient := &http.Client{}');
45+
code.push(util.format('\turl := "%s"', source.fullUrl));
46+
47+
// If we have body content or not create the var and reader or nil
48+
if (source.postData.text) {
49+
code.push('\tpayload := ' + JSON.stringify(source.postData.text));
50+
code.push(util.format('\treq, ' + errorPlaceholder + ' := http.NewRequest("%s", url, strings.NewReader(payload))', source.method));
51+
} else {
52+
code.push(util.format('\treq, %s := http.NewRequest("%s", url, nil)', errorPlaceholder, source.method));
53+
}
54+
55+
errorCheck();
56+
57+
// Add headers
58+
Object.keys(source.allHeaders).map(function (key) {
59+
code.push(util.format('\treq.Header.Add("%s", "%s")', key, source.allHeaders[key]));
60+
});
61+
62+
// Make request
63+
code.push(util.format('\tres, %s := client.Do(req)', errorPlaceholder));
64+
errorCheck();
65+
66+
// Get Body
67+
if (opts.printBody) {
68+
code.push('\tdefer res.Body.Close()');
69+
}
70+
71+
if (opts.printBody) {
72+
code.push(util.format('\tbody, %s := ioutil.ReadAll(res.Body)', errorPlaceholder));
73+
}
74+
75+
errorCheck();
76+
77+
// Print it
78+
code.push('\tfmt.Println(res)');
79+
80+
if (opts.printBody) {
81+
code.push('\tfmt.Println(string(body))');
82+
}
83+
84+
// End main block
85+
code.push('}');
86+
87+
return code.join('\n');
88+
};
89+
90+
module.exports.info = {
91+
key: 'native',
92+
title: 'NewRequest',
93+
link: 'http://golang.org/pkg/net/http/#NewRequest',
94+
description: 'Golang HTTP client request'
95+
};

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,19 @@
8787
"description": "Foundation's NSURLSession request"
8888
}
8989
]
90+
},
91+
{
92+
"key": "go",
93+
"title": "Go",
94+
"extname": ".go",
95+
"default": "native",
96+
"clients": [
97+
{
98+
"key": "native",
99+
"title": "NewRequest",
100+
"link": "http://golang.org/pkg/net/http/#NewRequest",
101+
"description": "Golang HTTP client request"
102+
}
103+
]
90104
}
91105
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "foo=bar&hello=world"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("content-type", "application/x-www-form-urlencoded")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "{\"foo\": \"bar\"}"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("content-type", "application/json")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"io/ioutil"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
url := "http://mockbin.com/har"
12+
req, _ := http.NewRequest("POST", url, nil)
13+
req.Header.Add("cookie", "foo=bar; bar=baz")
14+
res, _ := client.Do(req)
15+
defer res.Body.Close()
16+
body, _ := ioutil.ReadAll(res.Body)
17+
fmt.Println(res)
18+
fmt.Println(string(body))
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har?foo=bar&foo=baz&baz=abc&key=value"
13+
payload := "foo=bar"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("cookie", "foo=bar; bar=baz")
16+
req.Header.Add("accept", "application/json")
17+
req.Header.Add("content-type", "application/x-www-form-urlencoded")
18+
res, _ := client.Do(req)
19+
defer res.Body.Close()
20+
body, _ := ioutil.ReadAll(res.Body)
21+
fmt.Println(res)
22+
fmt.Println(string(body))
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"io/ioutil"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
url := "http://mockbin.com/har"
12+
req, _ := http.NewRequest("GET", url, nil)
13+
req.Header.Add("accept", "application/json")
14+
req.Header.Add("x-foo", "Bar")
15+
res, _ := client.Do(req)
16+
defer res.Body.Close()
17+
body, _ := ioutil.ReadAll(res.Body)
18+
fmt.Println(res)
19+
fmt.Println(string(body))
20+
}

0 commit comments

Comments
 (0)