Skip to content

Commit 823718d

Browse files
committed
Merge branch 'master' of github.com:ahmadnassri/httpsnippet
Conflicts: test/index.js
2 parents 8a131c0 + 2cf662c commit 823718d

8 files changed

Lines changed: 172 additions & 20 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ process single file (assumes [HAR Request Object](http://www.softwareishard.com/
9595
}
9696
```
9797
```shell
98-
httpsnippet my-api-endpoint.json --langauge php --output ./snippets
98+
httpsnippet my-api-endpoint.json --target php --output ./snippets
9999
```
100100

101101
```shell
@@ -107,7 +107,7 @@ snippets/
107107
process multiple files:
108108

109109
```shell
110-
httpsnippet /*.json --langauge nodejs --output ./snippets
110+
httpsnippet /*.json --family node --target native --output ./snippets
111111
```
112112

113113
```shell

src/targets/objc/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/objc/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: 'objc',
5+
title: 'Objective-C',
6+
extname: '.m',
7+
default: 'native'
8+
};

src/targets/objc/native.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
module.exports = function (options) {
4+
var code = [];
5+
6+
// Dependencies
7+
code.push('#import <Foundation/Foundation.h>');
8+
code.push(null);
9+
code.push('NSURLSession *session = [NSURLSession sharedSession];');
10+
code.push(null);
11+
// Create request object
12+
code.push('NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"' + this.source.fullUrl + '"]');
13+
code.push(' cachePolicy:NSURLRequestUseProtocolCachePolicy');
14+
code.push(' timeoutInterval:10.0];');
15+
code.push('[request setHTTPMethod:@"' + this.source.method + '"];');
16+
17+
// Set headers
18+
this.source.headers.forEach(function (header) {
19+
code.push('[request setValue:@"' + header.value + '" forHTTPHeaderField:@"' + header.name + '"];');
20+
});
21+
22+
var cookies = this.source.cookies.map(function (cookie) {
23+
return encodeURIComponent(cookie.name) + '=' + encodeURIComponent(cookie.value);
24+
});
25+
26+
if (cookies.length) {
27+
code.push('[request setValue:@"' + cookies.join('; ') + '" forHTTPHeaderField:@"Cookie"];');
28+
}
29+
30+
// Set request body
31+
if (this.source.postData && this.source.postData.params || this.source.postData.text) {
32+
code.push(null);
33+
34+
if (this.source.postData.mimeType === 'application/x-www-form-urlencoded' && this.source.postData.params) {
35+
var params = this.source.postData.params;
36+
code.push('NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"' + params[0].name + '=' + params[0].value + '" dataUsingEncoding:NSUTF8StringEncoding]];');
37+
for (var i = 1, len = params.length; i < len; i++) {
38+
code.push('[postData appendData:[@"&' + params[i].name + '=' + params[i].value + '" dataUsingEncoding:NSUTF8StringEncoding]];');
39+
}
40+
} else if (this.source.postData.mimeType === 'application/json' && this.source.postData.text) {
41+
code.push('NSData *postData = [[NSData alloc] initWithData:[@' + JSON.stringify(this.source.postData.text) + ' dataUsingEncoding:NSUTF8StringEncoding]];');
42+
} else if (this.source.postData.text) {
43+
code.push('NSData *postData = [[NSData alloc] initWithData:[@"' + this.source.postData.text + '" dataUsingEncoding:NSUTF8StringEncoding]];');
44+
}
45+
46+
code.push('[request setHTTPBody:postData];');
47+
}
48+
49+
// Set completion block
50+
code.push(null);
51+
code.push('NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request');
52+
code.push(' completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {');
53+
code.push(null);
54+
code.push('}];');
55+
56+
// Start request
57+
code.push(null);
58+
code.push('[dataTask resume];');
59+
60+
return code.join('\n');
61+
};
62+
63+
module.exports.info = {
64+
key: 'native',
65+
title: 'NSURLSession',
66+
link: 'https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html',
67+
description: 'Foundation\'s NSURLSession request'
68+
};

test/fixtures/available-targets.json

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
"description": "a CLI, cURL-like tool for humans",
1414
"extname": ".sh"
1515
},
16+
{
17+
"key": "wget",
18+
"title": "Wget",
19+
"link": "https://www.gnu.org/software/wget/",
20+
"description": "a free software package for retrieving files using HTTP, HTTPS",
21+
"extname": ".sh"
22+
},
1623
{
1724
"key": "node",
1825
"title": "Node.JS",
@@ -54,11 +61,19 @@
5461
"description": "PHP with libcurl"
5562
}
5663
]
57-
}, {
58-
"key": "wget",
59-
"title": "Wget",
60-
"link": "https://www.gnu.org/software/wget/",
61-
"description": "a free software package for retrieving files using HTTP, HTTPS",
62-
"extname": ".sh"
64+
},
65+
{
66+
"key": "objc",
67+
"title": "Objective-C",
68+
"extname": ".m",
69+
"default": "native",
70+
"clients": [
71+
{
72+
"key": "native",
73+
"title": "NSURLSession",
74+
"link": "https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/index.html",
75+
"description": "Foundation's NSURLSession request"
76+
}
77+
]
6378
}
6479
]

test/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var HTTPSnippet = require('../src');
55
var should = require('should');
66

77
describe('HTTPSnippet', function () {
8-
it('should list all available targets', function (done) {
9-
var targets = HTTPSnippet.availableTargets();
8+
var targets = HTTPSnippet.availableTargets();
109

11-
targets.should.be.an.Array;
12-
targets.should.eql(fixtures['available-targets']);
13-
14-
done();
10+
targets.map(function (target) {
11+
it('availableTargets should include ' + target.title, function (done) {
12+
fixtures['available-targets'].should.containEql(target);
13+
done();
14+
});
1515
});
1616

1717
it('should add "uriObj" to source object', function (done) {

test/targets.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ var clearInfo = function (key, cb) {
2121
cb(!~['info', 'index'].indexOf(key));
2222
};
2323

24-
var itShouldHaveTests = function (test, key) {
24+
var itShouldHaveTests = function (test, func, key) {
2525
it(key + ' should have tests', function (done) {
26-
test.should.be.exist;
26+
test.should.have.property(func);
2727
done();
2828
});
2929
};
@@ -44,7 +44,7 @@ var itShouldHaveInfo = function (targets, key) {
4444

4545
var itShouldHaveRequestTestOutputFixture = function (request, target, path) {
4646
it('should have output test for ' + request, function (done) {
47-
Object.keys(output).should.containEql(target + '/' + path + request + snippet.extname(target));
47+
Object.keys(output).indexOf(target + '/' + path + request + snippet.extname(target)).should.be.greaterThan(-1);
4848

4949
done();
5050
});
@@ -66,12 +66,23 @@ var itShouldGenerateOutput = function (request, path, target, client) {
6666
}
6767
};
6868

69+
describe('Available Targets', function () {
70+
var targets = snippet.availableTargets();
71+
72+
targets.map(function (target) {
73+
it('available-targets.json should include ' + target.title, function (done) {
74+
fixtures['available-targets'].should.containEql(target);
75+
done();
76+
});
77+
});
78+
});
79+
6980
// test all the things!
7081
async.each(Object.keys(targets), function (target) {
7182
describe(targets[target].info.title, function () {
7283
itShouldHaveInfo(targets, target);
7384

74-
itShouldHaveTests(tests[target], target);
85+
itShouldHaveTests(tests, target, target);
7586

7687
if (typeof tests[target] === 'function') {
7788
tests[target](snippet, fixtures);
@@ -94,9 +105,9 @@ async.each(Object.keys(targets), function (target) {
94105
describe(client, function () {
95106
itShouldHaveInfo(targets[target], client);
96107

97-
itShouldHaveTests(tests[target][client], client);
108+
itShouldHaveTests(tests[target], client, client);
98109

99-
if (typeof tests[target][client] === 'function') {
110+
if (tests[target] && typeof tests[target][client] === 'function') {
100111
tests[target][client](snippet, fixtures);
101112
}
102113

test/targets/objc/native.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
var fixtures = require('../../fixtures');
4+
var HTTPSnippet = require('../../../src');
5+
var should = require('should');
6+
7+
describe('Objective-C', function () {
8+
it('should convert full request to an Obj-c snippet', function (done) {
9+
var result = new HTTPSnippet(fixtures.full).convert('objc', 'native');
10+
11+
result.replace(/\n/g, '').should.eql('#import <Foundation/Foundation.h>NSURLSession *session = [NSURLSession sharedSession];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/request?baz=abc&foo=bar&foo=baz"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];[request setHTTPMethod:@"POST"];[request setValue:@"text/plain" forHTTPHeaderField:@"Accept"];[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];[request setValue:@"2" forHTTPHeaderField:@"X-Pretty-Print"];[request setValue:@"foo=bar; bar=baz" forHTTPHeaderField:@"Cookie"];NSData *postData = [[NSData alloc] initWithData:[@"{\\"foo\\": \\"bar\\"}" dataUsingEncoding:NSUTF8StringEncoding]];[request setHTTPBody:postData];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];[dataTask resume];');
12+
13+
done();
14+
});
15+
16+
it('should convert query request to an OCaml snippet', function (done) {
17+
var result = new HTTPSnippet(fixtures.query).convert('objc', 'native');
18+
19+
result.replace(/\n/g, '').should.eql('#import <Foundation/Foundation.h>NSURLSession *session = [NSURLSession sharedSession];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/request?key=value&baz=abc&foo=bar&foo=baz"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];[request setHTTPMethod:@"POST"];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];[dataTask resume];');
20+
21+
done();
22+
});
23+
24+
it('should convert simple request to an OCaml snippet', function (done) {
25+
var result = new HTTPSnippet(fixtures.simple).convert('objc', 'native');
26+
27+
result.replace(/\n/g, '').should.eql('#import <Foundation/Foundation.h>NSURLSession *session = [NSURLSession sharedSession];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/request?foo=bar"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];[request setHTTPMethod:@"POST"];[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];[request setValue:@"bar=baz" forHTTPHeaderField:@"Cookie"];NSData *postData = [[NSData alloc] initWithData:[@"{\\"foo\\": \\"bar\\"}" dataUsingEncoding:NSUTF8StringEncoding]];[request setHTTPBody:postData];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];[dataTask resume];');
28+
29+
done();
30+
});
31+
32+
it('should convert short request to an OCaml snippet', function (done) {
33+
var result = new HTTPSnippet(fixtures.short).convert('objc', 'native');
34+
35+
result.replace(/\n/g, '').should.eql('#import <Foundation/Foundation.h>NSURLSession *session = [NSURLSession sharedSession];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/echo"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];[request setHTTPMethod:@"GET"];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];[dataTask resume];');
36+
37+
done();
38+
});
39+
40+
it('should convert http1 request to an OCaml snippet', function (done) {
41+
var result = new HTTPSnippet(fixtures.http1).convert('objc', 'native');
42+
43+
result.replace(/\n/g, '').should.eql('#import <Foundation/Foundation.h>NSURLSession *session = [NSURLSession sharedSession];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mockbin.com/request"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];[request setHTTPMethod:@"GET"];NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];[dataTask resume];');
44+
45+
done();
46+
});
47+
});

0 commit comments

Comments
 (0)