Skip to content

Commit e145fca

Browse files
committed
Merge pull request Kong#14 from thibaultCha/fix/tests
Fix: objc/native tests + enhancements
2 parents 937dc5a + 80a094e commit e145fca

25 files changed

Lines changed: 223 additions & 54 deletions

‎src/targets/objc/native.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22

3+
var util = require('util');
4+
35
module.exports = function (options) {
6+
var opts = util._extend({
7+
timeout: '10'
8+
}, options);
9+
410
var code = [];
511

612
// Dependencies
@@ -11,7 +17,7 @@ module.exports = function (options) {
1117
// Create request object
1218
code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + this.source.fullUrl + '"]');
1319
code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy');
14-
code.push(' timeoutInterval:10.0];');
20+
code.push(' timeoutInterval:' + parseInt(opts.timeout, 10).toFixed(1) + '];');
1521
code.push('[request setHTTPMethod:@"' + this.source.method + '"];');
1622

1723
// Set headers
@@ -28,7 +34,7 @@ module.exports = function (options) {
2834
}
2935

3036
// Set request body
31-
if (this.source.postData && this.source.postData.params || this.source.postData.text) {
37+
if (this.source.postData && (this.source.postData.params || this.source.postData.text)) {
3238
code.push(null);
3339

3440
if (this.source.postData.mimeType === 'application/x-www-form-urlencoded' && this.source.postData.params) {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
curl --request POST \
22
--url "http://mockbin.com/har" \
33
--header "Content-Type: application/x-www-form-urlencoded" \
4-
--form "foo=bar"
4+
--form "foo=bar" \
5+
--form "hello=world"

‎test/fixtures/output/curl/http1.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
curl --request GET \
2+
--url "http://mockbin.com/request" \
3+
--http1.0
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
http POST http://mockbin.com/har \
22
Content-Type:application/x-www-form-urlencoded \
3-
foo:bar
3+
foo:bar \
4+
hello:world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http GET http://mockbin.com/request

‎test/fixtures/output/node/native/application-form-encoded.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ var req = http.request(options, function (res) {
2323
});
2424
});
2525

26-
var postData = querystring.stringify({"foo":"bar"});
26+
var postData = querystring.stringify({"foo":"bar","hello":"world"});
2727
req.write(postData);
2828
req.end();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var http = require("http");
2+
3+
var options = {
4+
"method": "GET",
5+
"hostname": "mockbin.com",
6+
"port": null,
7+
"path": "/request?",
8+
"headers": {}
9+
};
10+
11+
var req = http.request(options, function (res) {
12+
var chunks = [];
13+
14+
res.on("data", function (chunk) {
15+
chunks.push(chunk);
16+
});
17+
18+
res.on("end", function () {
19+
var body = Buffer.concat(chunks);
20+
});
21+
});
22+
23+
req.end();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NSURLSession *session = [NSURLSession sharedSession];
4+
5+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har"]
6+
cachePolicy:NSURLRequestUseProtocolCachePolicy
7+
timeoutInterval:10.0];
8+
[request setHTTPMethod:@"POST"];
9+
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
10+
11+
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"foo=bar" dataUsingEncoding:NSUTF8StringEncoding]];
12+
[postData appendData:[@"&hello=world" dataUsingEncoding:NSUTF8StringEncoding]];
13+
[request setHTTPBody:postData];
14+
15+
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
16+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
17+
18+
}];
19+
20+
[dataTask resume];
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NSURLSession *session = [NSURLSession sharedSession];
4+
5+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har"]
6+
cachePolicy:NSURLRequestUseProtocolCachePolicy
7+
timeoutInterval:10.0];
8+
[request setHTTPMethod:@"POST"];
9+
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
10+
11+
NSData *postData = [[NSData alloc] initWithData:[@"{\"foo\": \"bar\"}" dataUsingEncoding:NSUTF8StringEncoding]];
12+
[request setHTTPBody:postData];
13+
14+
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
15+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
16+
17+
}];
18+
19+
[dataTask resume];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NSURLSession *session = [NSURLSession sharedSession];
4+
5+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/har"]
6+
cachePolicy:NSURLRequestUseProtocolCachePolicy
7+
timeoutInterval:10.0];
8+
[request setHTTPMethod:@"POST"];
9+
[request setValue:@"foo=bar; bar=baz" forHTTPHeaderField:@"Cookie"];
10+
11+
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
12+
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
13+
14+
}];
15+
16+
[dataTask resume];

0 commit comments

Comments
 (0)