AFNetworking/Tests/AFJSONRequestOperationTests.m

101 lines
4.6 KiB
Mathematica
Raw Normal View History

2013-05-22 09:37:23 -07:00
// AFJSONRequestOperationTests.m
2013-05-16 14:14:00 -05:00
//
2013-05-22 09:37:23 -07:00
// Copyright (c) 2013 AFNetworking (http://afnetworking.com)
2013-05-16 14:14:00 -05:00
//
2013-05-22 09:37:23 -07:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2013-05-16 14:14:00 -05:00
//
2013-05-22 09:37:23 -07:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
2013-05-16 14:14:00 -05:00
#import "AFNetworkingTests.h"
@interface AFJSONRequestOperationTests : SenTestCase
@property (readwrite, nonatomic, strong) NSURL *baseURL;
@end
@implementation AFJSONRequestOperationTests
@synthesize baseURL = _baseURL;
- (void)setUp {
self.baseURL = [NSURL URLWithString:AFNetworkingTestsBaseURLString];
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONRequestOperationAcceptsApplicationJSON {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=application/json" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.error).will.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONRequestOperationAcceptsTextJSON {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/json" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.error).will.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONRequestOperationAcceptsTextJavascript {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=text/javascript" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.error).will.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONRequestOperationAcceptsCustomContentType {
2013-05-16 14:26:34 -05:00
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"application/customjson"]];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=application/customjson" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:26:34 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.error).will.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONRequestOperationDoesNotAcceptInvalidContentType {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=application/no-json" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.error).willNot.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONResponseObjectIsNotNilWhenValidJSONIsReturned {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/response-headers?Content-Type=application/json" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.responseJSON).willNot.beNil();
}
2013-05-22 09:37:23 -07:00
- (void)testThatJSONResponseObjectIsNilWhenErrorOccurs {
2013-05-16 14:14:00 -05:00
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/status/404" relativeToURL:self.baseURL]];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
2013-05-22 09:37:23 -07:00
2013-05-16 14:14:00 -05:00
[operation start];
expect([operation isFinished]).will.beTruthy();
expect(operation.responseJSON).will.beNil();
}
@end