Skip to content

Commit 4224606

Browse files
committed
fix: objc native body condition and test describes
1 parent 5373e1a commit 4224606

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎src/targets/objc/native.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = function (options) {
2828
}
2929

3030
// Set request body
31-
if (this.source.postData && this.source.postData.params || this.source.postData.text) {
31+
if (this.source.postData && (this.source.postData.params || this.source.postData.text)) {
3232
code.push(null);
3333

3434
if (this.source.postData.mimeType === 'application/x-www-form-urlencoded' && this.source.postData.params) {

‎test/tests/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 a native Objective-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 a native Objective-C 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 a native Objective-C 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 a native Objective-C 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 a native Objective-C 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)