Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP alterations in SDLURLSession to use NSURLComponents #436

Merged
merged 5 commits into from Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions SmartDeviceLink/SDLURLSession.m
Expand Up @@ -64,7 +64,7 @@ - (instancetype)init {
- (void)dataFromURL:(NSURL *)url completionHandler:(SDLURLConnectionRequestCompletionHandler)completionHandler {
// Apple no longer allows HTTP URLs without a special exception as of Jan. 2017
if ([url.scheme isEqualToString:@"http"]) {
url = [NSURL URLWithString:[url.absoluteString stringByReplacingOccurrencesOfString:@"http" withString:@"https"]];
url = [NSURL URLWithString:[url.absoluteString stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"https"]];
}

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:self.cachePolicy timeoutInterval:self.connectionTimeout];
Expand All @@ -78,7 +78,7 @@ - (void)dataFromURL:(NSURL *)url completionHandler:(SDLURLConnectionRequestCompl
- (void)uploadWithURLRequest:(NSURLRequest *)request data:(NSData *)data completionHandler:(SDLURLConnectionRequestCompletionHandler)completionHandler {
NSURL *newURL = nil;
if ([request.URL.scheme isEqualToString:@"http"]) {
newURL = [NSURL URLWithString:[request.URL.absoluteString stringByReplacingOccurrencesOfString:@"http" withString:@"https"]];
newURL = [NSURL URLWithString:[request.URL.absoluteString stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"https"]];
}

NSMutableURLRequest *mutableRequest = [request mutableCopy];
Expand Down
Expand Up @@ -10,97 +10,119 @@
__block SDLURLSession *testSession = nil;

describe(@"attempting to get good data", ^{
context(@"uploading data", ^{
context(@"from an http address", ^{
context(@"uploading data", ^{
NSData *testData = [@"testData" dataUsingEncoding:NSUTF8StringEncoding];
NSArray *someJSONObject = @[@"one", @"two"];
NSData *testJSONData = [NSJSONSerialization dataWithJSONObject:someJSONObject options:0 error:nil];

__block NSData *testReturnData = nil;
__block NSURLResponse *testReturnResponse = nil;
__block NSError *testReturnError = nil;
__block NSArray *testReturnJSONObject = nil;

beforeEach(^{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
if ([request.URL.host isEqualToString:@"www.faketest.com"]) {
testReturnJSONObject = [NSJSONSerialization JSONObjectWithData:request.HTTPBody options:0 error:nil];
return YES;
}

return NO;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:testData statusCode:200 headers:nil] requestTime:0.5 responseTime:0];
}];

testSession = [[SDLURLSession alloc] init];
NSURLRequest *someURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.faketest.com"]];

[testSession uploadWithURLRequest:someURLRequest data:testJSONData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
testReturnData = data;
testReturnResponse = response;
testReturnError = error;
}];
});

afterEach(^{
testSession = nil;
[OHHTTPStubs removeAllStubs];
});

it(@"should have correct data", ^{
expect(testReturnJSONObject).toEventually(equal(someJSONObject));
expect(testReturnData).toEventually(equal(testData));
expect(testReturnResponse).toEventuallyNot(beNil());
expect(testReturnError).toEventually(beNil());
});
});

context(@"downloading data", ^{
NSData *testData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding];

__block NSData *testReturnData = nil;
__block NSURLResponse *testReturnResponse = nil;
__block NSError *testReturnError = nil;

beforeEach(^{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"www.faketest.com"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:testData statusCode:200 headers:nil] requestTime:0.5 responseTime:0];
}];

testSession = [[SDLURLSession alloc] init];
[testSession dataFromURL:[NSURL URLWithString:@"https://www.faketest.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
testReturnData = data;
testReturnResponse = response;
testReturnError = error;
}];
});

afterEach(^{
testSession = nil;
[OHHTTPStubs removeAllStubs];
});

it(@"should return correct info", ^{
expect(testReturnData).toEventually(equal(testData));
expect(testReturnResponse).toEventuallyNot(beNil());
expect(testReturnError).toEventually(beNil());
});
});
});

context(@"from an http address", ^{
NSData *testData = [@"testData" dataUsingEncoding:NSUTF8StringEncoding];
NSArray *someJSONObject = @[@"one", @"two"];
NSData *testJSONData = [NSJSONSerialization dataWithJSONObject:someJSONObject options:0 error:nil];

__block NSData *testReturnData = nil;
__block NSURLResponse *testReturnResponse = nil;
__block NSError *testReturnError = nil;
__block NSArray *testReturnJSONObject = nil;
__block NSString *testURLRequestComponent = nil;

beforeEach(^{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
if ([request.URL.host isEqualToString:@"www.faketest.com"]) {
testReturnJSONObject = [NSJSONSerialization JSONObjectWithData:request.HTTPBody options:0 error:nil];
testURLRequestComponent = request.URL.scheme;
return YES;
}

return NO;
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:testData statusCode:200 headers:nil] requestTime:0.5 responseTime:0];
return [[OHHTTPStubsResponse responseWithData:testData statusCode:200 headers:request.allHTTPHeaderFields] requestTime:0.5 responseTime:0];
}];

testSession = [[SDLURLSession alloc] init];
NSURLRequest *someURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.faketest.com"]];

[testSession uploadWithURLRequest:someURLRequest data:testJSONData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
testReturnData = data;
testReturnResponse = response;
testReturnError = error;
}];
});

afterEach(^{
testSession = nil;
[OHHTTPStubs removeAllStubs];
});

it(@"should have the json object", ^{
expect(testReturnJSONObject).toEventually(equal(someJSONObject));
});

it(@"should not return any data", ^{
expect(testReturnData).toEventually(equal(testData));
});

it(@"should return a response", ^{
expect(testReturnResponse).toEventuallyNot(beNil());
});

it(@"should not return an error", ^{
expect(testReturnError).toEventually(beNil());
});
});

context(@"downloading data", ^{
NSData *testData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding];

__block NSData *testReturnData = nil;
__block NSURLResponse *testReturnResponse = nil;
__block NSError *testReturnError = nil;

beforeEach(^{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return [request.URL.host isEqualToString:@"www.faketest.com"];
} withStubResponse:^OHHTTPStubsResponse *(NSURLRequest *request) {
return [[OHHTTPStubsResponse responseWithData:testData statusCode:200 headers:nil] requestTime:0.5 responseTime:0];
}];

testSession = [[SDLURLSession alloc] init];
[testSession dataFromURL:[NSURL URLWithString:@"http://www.faketest.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
testReturnData = data;
testReturnResponse = response;
testReturnError = error;
}];
[testSession uploadWithURLRequest:someURLRequest data:testJSONData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {}];
});

afterEach(^{
testSession = nil;
[OHHTTPStubs removeAllStubs];
});

it(@"should not return any data", ^{
expect(testReturnData).toEventually(equal(testData));
});

it(@"should return a response", ^{
expect(testReturnResponse).toEventuallyNot(beNil());
});

it(@"should not return an error", ^{
expect(testReturnError).toEventually(beNil());
it(@"should have called the HTTPS URL instead", ^{
expect(testURLRequestComponent).toEventually(match(@"https"));
});
});
});
Expand Down Expand Up @@ -134,13 +156,7 @@

it(@"should return nil data", ^{
expect(testReturnData).toEventually(beNil());
});

it(@"should return a nil response", ^{
expect(testReturnResponse).toEventually(beNil());
});

it(@"should return an error", ^{
expect(@(testReturnError.code)).toEventually(equal(@(someNetworkError.code)));
});
});
Expand Down Expand Up @@ -176,13 +192,7 @@

it(@"should return nil data", ^{
expect(testReturnData).toEventually(beNil());
});

it(@"should return a nil response", ^{
expect(testReturnResponse).toEventually(beNil());
});

it(@"should return an error", ^{
expect(@(testReturnError.code)).toEventually(equal(@(kCFURLErrorCancelled)));
});
});
Expand Down