Skip to content

Commit 4c3dbd1

Browse files
author
Montana Flynn
committed
Add Golang
1 parent 81e89bc commit 4c3dbd1

File tree

13 files changed

+218
-0
lines changed

13 files changed

+218
-0
lines changed

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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function (options) {
6+
var opts = util._extend({
7+
indent: ' '
8+
}, options);
9+
10+
// Set helper object for later
11+
var req = {
12+
url: this.source.fullUrl,
13+
method: this.source.method,
14+
hostname: this.source.uriObj.hostname,
15+
port: this.source.uriObj.port,
16+
path: this.source.uriObj.path,
17+
headers: this.source.headersObj
18+
};
19+
20+
// Find out if we have body content or not
21+
var bodyPresent = this.source.postData && this.source.postData.text;
22+
if (bodyPresent) {
23+
req.body = 'strings.NewReader(' + JSON.stringify(this.source.postData.text) + ')';
24+
} else {
25+
req.body = "nil";
26+
}
27+
28+
// Start writing code out
29+
var code = [];
30+
31+
// Create boilerplate
32+
code.push('package main\n');
33+
code.push('import (');
34+
code.push('\t"fmt"');
35+
if (bodyPresent) code.push('\t"strings"');
36+
code.push('\t"net/http"');
37+
code.push(')\n');
38+
39+
code.push('func main() {')
40+
41+
// Create client
42+
code.push('\tclient := &http.Client{}');
43+
code.push('\treq, _ := http.NewRequest("' + req.method + '", "' + req.url + '", ' + req.body + ')');
44+
45+
// Add headers
46+
var headersPresent = this.source.headers && this.source.headers.length;
47+
48+
if (headersPresent) {
49+
for (var header in this.source.headers) {
50+
var key = this.source.headers[header].name
51+
var val = this.source.headers[header].value
52+
code.push('\treq.Header.Add("' + key + '", "' + val + '")');
53+
}
54+
}
55+
56+
// Add Cookies
57+
var cookiesPresent = this.source.cookies && this.source.cookies.length;
58+
if (cookiesPresent) {
59+
var cookies = this.source.cookies.map(function (cookie) {
60+
return cookie.name + '=' + cookie.value;
61+
}).join('; ');
62+
code.push('\treq.Header.Add("Cookie", "' + cookies + '")');
63+
}
64+
65+
// Make request
66+
code.push('\tres, _ := client.Do(req)');
67+
68+
// Print it
69+
code.push('\tfmt.Printf("%+v", res)');
70+
71+
// End main block
72+
code.push('}')
73+
74+
return code.join('\n');
75+
};
76+
77+
module.exports.info = {
78+
key: 'native',
79+
title: 'NewRequest',
80+
link: 'http://golang.org/pkg/net/http/#NewRequest',
81+
description: 'Golang HTTP client request'
82+
};

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,19 @@
8181
"description": "Foundation's NSURLSession request"
8282
}
8383
]
84+
},
85+
{
86+
"key": "go",
87+
"title": "Go",
88+
"extname": ".go",
89+
"default": "native",
90+
"clients": [
91+
{
92+
"key": "native",
93+
"title": "NewRequest",
94+
"link": "http://golang.org/pkg/net/http/#NewRequest",
95+
"description": "Golang HTTP client request"
96+
}
97+
]
8498
}
8599
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
req, _ := http.NewRequest("POST", "http://mockbin.com/har", strings.NewReader("foo=bar&hello=world"))
12+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
13+
res, _ := client.Do(req)
14+
fmt.Printf("%+v", res)
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
req, _ := http.NewRequest("POST", "http://mockbin.com/har", strings.NewReader("{\"foo\": \"bar\"}"))
12+
req.Header.Add("Content-Type", "application/json")
13+
res, _ := client.Do(req)
14+
fmt.Printf("%+v", res)
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func main() {
9+
client := &http.Client{}
10+
req, _ := http.NewRequest("POST", "http://mockbin.com/har", nil)
11+
req.Header.Add("Cookie", "foo=bar; bar=baz")
12+
res, _ := client.Do(req)
13+
fmt.Printf("%+v", res)
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
req, _ := http.NewRequest("POST", "http://mockbin.com/har?baz=abc&foo=bar&foo=baz", strings.NewReader("foo=bar"))
12+
req.Header.Add("Accept", "application/json")
13+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
14+
req.Header.Add("Cookie", "foo=bar; bar=baz")
15+
res, _ := client.Do(req)
16+
fmt.Printf("%+v", res)
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func main() {
9+
client := &http.Client{}
10+
req, _ := http.NewRequest("GET", "http://mockbin.com/har", nil)
11+
req.Header.Add("Accept", "application/json")
12+
req.Header.Add("X-Foo", "Bar")
13+
res, _ := client.Do(req)
14+
fmt.Printf("%+v", res)
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func main() {
9+
client := &http.Client{}
10+
req, _ := http.NewRequest("GET", "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz", nil)
11+
res, _ := client.Do(req)
12+
fmt.Printf("%+v", res)
13+
}

0 commit comments

Comments
 (0)