Skip to content

Commit 6f086ed

Browse files
committed
feat: objective-c support (native NSURLSession)
Support for Foundation's NSURLSession (iOS 7/Mac OS X 10.9) and tests. Target (language key) is 'objc'. It doesn't support the 'indent' option since nothing is indented strictly speaking. The only 'indentation' is Objective-C's syntax making some method invocations to take multiple lines.
1 parent cb93bc7 commit 6f086ed

4 files changed

Lines changed: 133 additions & 1 deletion

File tree

src/targets/objc/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = require('requireindex')(__dirname);
4+
5+
module.exports._familyInfo = function () {
6+
return {
7+
key: 'objc',
8+
title: 'Objective-C',
9+
extname: '.m',
10+
default: 'native'
11+
};
12+
};

src/targets/objc/native.js

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

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('HTTPSnippet', function () {
99
var targets = HTTPSnippet._targets().sort();
1010

1111
targets.should.be.an.Array;
12-
targets.should.eql(['curl', 'httpie', 'node', 'ocaml', 'php', 'wget']);
12+
targets.should.eql(['curl', 'httpie', 'node', 'objc', 'ocaml', 'php', 'wget']);
1313

1414
done();
1515
});

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)