Permalink
Browse files

Ability for client to perform requests

  • Loading branch information...
1 parent af632e2 commit b277f7e6cce3ba4965f1308daf3ad51ed7da2f0c @sebreh sebreh committed Jan 21, 2014
@@ -24,6 +24,7 @@
5014C583188817550002C68D /* PKTClientTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5014C55B188806E50002C68D /* PKTClientTests.m */; };
5014C585188817550002C68D /* XCTestProbe+PKTRegisterObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 50485961188541B60088AF2C /* XCTestProbe+PKTRegisterObserver.m */; };
5014C58818881E070002C68D /* PKTCoverageTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 5014C58618881E070002C68D /* PKTCoverageTestObserver.m */; };
+ 5032B0B6188EB6DC0080046B /* PKTRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5032B0B5188EB6DC0080046B /* PKTRequestTests.m */; };
50485955188461CF0088AF2C /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50485954188461CF0088AF2C /* XCTest.framework */; };
505C4F99155C96C30081850F /* DemoApp-Configuration.plist in Resources */ = {isa = PBXBuildFile; fileRef = 505C4F98155C96C30081850F /* DemoApp-Configuration.plist */; };
50620AAC14BC8CA60060A786 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 50620AAD14BC8CA60060A786 /* InfoPlist.strings */; };
@@ -156,6 +157,8 @@
5030C0C51435E92800027F07 /* PKStreamDataFactory.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PKStreamDataFactory.m; sourceTree = "<group>"; };
5032336014FD3F2C0028D887 /* PKExternalFileData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PKExternalFileData.h; sourceTree = "<group>"; };
5032336114FD3F2C0028D887 /* PKExternalFileData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PKExternalFileData.m; sourceTree = "<group>"; };
+ 5032B0B5188EB6DC0080046B /* PKTRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PKTRequestTests.m; sourceTree = "<group>"; };
+ 5032B0B7188ED0130080046B /* PKTClient+Test.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "PKTClient+Test.h"; sourceTree = "<group>"; };
5032BAB6144489CB00B7F7C7 /* PKBaseObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PKBaseObject.h; sourceTree = "<group>"; };
5032BAB7144489CB00B7F7C7 /* PKBaseObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PKBaseObject.m; sourceTree = "<group>"; };
5032BAC514448E1500B7F7C7 /* PKTestTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PKTestTask.h; sourceTree = "<group>"; };
@@ -740,6 +743,8 @@
5014C571188812540002C68D /* PKTAsyncTestCase.h */,
5014C572188812540002C68D /* PKTAsyncTestCase.m */,
5014C55B188806E50002C68D /* PKTClientTests.m */,
+ 5032B0B7188ED0130080046B /* PKTClient+Test.h */,
+ 5032B0B5188EB6DC0080046B /* PKTRequestTests.m */,
5048595C18853DC70088AF2C /* Observers */,
5014C55A188804A40002C68D /* V1 */,
5065BF0414335ABA00360855 /* Supporting Files */,
@@ -1304,6 +1309,7 @@
5014C57F188817430002C68D /* PKTRequest.m in Sources */,
5014C581188817430002C68D /* PKTResponse.m in Sources */,
5014C58818881E070002C68D /* PKTCoverageTestObserver.m in Sources */,
+ 5032B0B6188EB6DC0080046B /* PKTRequestTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -7,9 +7,16 @@
//
#import "PKTClient.h"
+#import "PKTRequest.h"
+#import "PKTResponse.h"
static NSString * const kDefaultBaseURLString = @"https://api.podio.com";
+static NSString * const kHTTPMethodGET = @"GET";
+static NSString * const kHTTPMethodPOST = @"POST";
+static NSString * const kHTTPMethodPUT = @"PUT";
+static NSString * const kHTTPMethodDELETE = @"DELETE";
+
@interface PKTClient ()
@property (nonatomic, copy, readwrite) NSString *apiKey;
@@ -33,9 +40,62 @@ - (instancetype)initWithAPIKey:(NSString *)key secret:(NSString *)secret {
return self;
}
+#pragma mark - Public
+
- (void)performRequest:(PKTRequest *)request completion:(PKTRequestCompletionBlock)completion {
+ NSURLSessionTask *task = [self taskWithRequest:request completion:completion];
+ [task resume];
+}
+
+#pragma mark - Private
+
++ (NSString *)HTTPMethodForMethod:(PKTRequestMethod)method {
+ NSString *string = nil;
+
+ switch (method) {
+ case PKTRequestMethodGET:
+ string = kHTTPMethodGET;
+ break;
+ case PKTRequestMethodPOST:
+ string = kHTTPMethodPOST;
+ break;
+ case PKTRequestMethodPUT:
+ string = kHTTPMethodPUT;
+ break;
+ case PKTRequestMethodDELETE:
+ string = kHTTPMethodDELETE;
+ break;
+ default:
+ break;
+ }
+ return string;
}
+- (NSURLRequest *)URLRequestForRequest:(PKTRequest *)request {
+ NSString *urlString = [[NSURL URLWithString:request.path relativeToURL:self.baseURL] absoluteString];
+ NSString *method = [[self class] HTTPMethodForMethod:request.method];
+
+ NSMutableURLRequest *urlRequest = [self.requestSerializer
+ requestWithMethod:method
+ URLString:urlString
+ parameters:request.parameters
+ error:nil];
+
+ return [urlRequest copy];
+}
+
+- (NSURLSessionTask *)taskWithRequest:(PKTRequest *)request completion:(PKTRequestCompletionBlock)completion {
+ NSURLRequest *urlRequest = [self URLRequestForRequest:request];
+
+ return [self dataTaskWithRequest:urlRequest completionHandler:^(NSURLResponse *urlResponse, id responseObject, NSError *error) {
+ PKTResponse *response = nil;
+ if (!error) {
+ response = [PKTResponse new];
+ }
+
+ if (completion) completion(response, error);
+ }];
+}
@end
View
@@ -8,6 +8,22 @@
#import <Foundation/Foundation.h>
+typedef NS_ENUM(NSUInteger, PKTRequestMethod) {
+ PKTRequestMethodGET,
+ PKTRequestMethodPOST,
+ PKTRequestMethodPUT,
+ PKTRequestMethodDELETE,
+};
+
@interface PKTRequest : NSObject
+@property (nonatomic, assign, readonly) PKTRequestMethod method;
+@property (nonatomic, copy, readonly) NSString *path;
+@property (nonatomic, copy, readonly) NSDictionary *parameters;
+
++ (instancetype)GETRequestWithPath:(NSString *)path;
++ (instancetype)POSTRequestWithPath:(NSString *)path;
++ (instancetype)PUTRequestWithPath:(NSString *)path;
++ (instancetype)DELETERequestWithPath:(NSString *)path;
+
@end
View
@@ -1,3 +1,4 @@
+
//
// PKTRequest.m
// PodioKit
@@ -10,4 +11,34 @@
@implementation PKTRequest
+- (instancetype)initWithPath:(NSString *)path method:(PKTRequestMethod)method {
+ self = [super init];
+ if (!self) return nil;
+
+ _path = [path copy];
+ _method = method;
+
+ return self;
+}
+
++ (instancetype)requestWithPath:(NSString *)path method:(PKTRequestMethod)method {
+ return [[self alloc] initWithPath:path method:method];
+}
+
++ (instancetype)GETRequestWithPath:(NSString *)path {
+ return [self requestWithPath:path method:PKTRequestMethodGET];
+}
+
++ (instancetype)POSTRequestWithPath:(NSString *)path {
+ return [self requestWithPath:path method:PKTRequestMethodPOST];
+}
+
++ (instancetype)PUTRequestWithPath:(NSString *)path {
+ return [self requestWithPath:path method:PKTRequestMethodPUT];
+}
+
++ (instancetype)DELETERequestWithPath:(NSString *)path {
+ return [self requestWithPath:path method:PKTRequestMethodDELETE];
+}
+
@end
@@ -9,7 +9,7 @@
#import "PKTAsyncTestCase.h"
-static NSTimeInterval const kDefaultTimeout = 10.0;
+static NSTimeInterval const kDefaultTimeout = 5.0;
@interface PKTAsyncTestCase ()
@@ -0,0 +1,15 @@
+//
+// PKTClient+Test.h
+// PodioKit
+//
+// Created by Sebastian Rehnby on 21/01/14.
+// Copyright (c) 2014 Citrix Systems, Inc. All rights reserved.
+//
+
+@interface PKTClient (Test)
+
++ (NSString *)HTTPMethodForMethod:(PKTRequestMethod)method;
+
+- (NSURLRequest *)URLRequestForRequest:(PKTRequest *)request;
+
+@end
@@ -7,40 +7,80 @@
//
#import <XCTest/XCTest.h>
+#import <OHHTTPStubs/OHHTTPStubs.h>
+#import "PKTAsyncTestCase.h"
#import "PKTClient.h"
#import "PKTRequest.h"
#import "PKTResponse.h"
+#import "PKTClient+Test.h"
-@interface PKTClientTests : XCTestCase
+static NSString * const kAPIKey = @"test-key";
+static NSString * const kAPISecret = @"test-secret";
+
+@interface PKTClientTests : PKTAsyncTestCase
+
+@property (nonatomic, strong) PKTClient *testClient;
@end
@implementation PKTClientTests
+- (void)setUp {
+ [super setUp];
+ self.testClient = [[PKTClient alloc] initWithAPIKey:kAPIKey secret:kAPISecret];
+}
+
+- (void)tearDown {
+ self.testClient = nil;
+ [super tearDown];
+}
+
+#pragma mark - Tests
+
- (void)testInit {
- NSString *key = @"some key";
- NSString *secret = @"secret";
- PKTClient *client = [[PKTClient alloc] initWithAPIKey:key secret:secret];
+ PKTClient *client = [[PKTClient alloc] initWithAPIKey:kAPISecret secret:kAPISecret];
- expect(client.apiKey).to.equal(key);
- expect(client.apiSecret).to.equal(secret);
+ expect(client.apiKey).to.equal(kAPISecret);
+ expect(client.apiSecret).to.equal(kAPISecret);
expect([client.baseURL absoluteString]).to.equal(@"https://api.podio.com");
}
-- (void)testPerformRequest {
-// PKTClient *client = [PKTClient initWithKey:@"some key" secret:@"secret"];
-//
-// PKTRequest *request = [PKTRequest GETRequestWithPath:@"/user/status"];
-//
-// [self waitForCompletion:^{
-// BOOL completed = NO;
-// [client performRequest:request completion:^(PKTResponse *result, NSError *error) {
-// completed = YES;
-// [self didFinish];
-// }];
-// }]
-//
-// expect(completed).to.beTrue;
+- (void)testPerformSuccessfulRequest {
+ PKTRequest *request = [PKTRequest GETRequestWithPath:@"/user/status"];
+
+ [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
+ return [request.URL.host isEqualToString:self.testClient.baseURL.host];
+ } withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
+ return [OHHTTPStubsResponse responseWithData:nil statusCode:200 headers:nil];
+ }];
+
+ __block BOOL completed = NO;
+ [self waitForCompletionWithBlock:^{
+ [self.testClient performRequest:request completion:^(PKTResponse *result, NSError *error) {
+ completed = YES;
+ [self finish];
+ }];
+ }];
+
+ expect(completed).to.beTruthy();
+}
+
+- (void)testHTTPMethod {
+ expect([PKTClient HTTPMethodForMethod:PKTRequestMethodGET]).to.equal(@"GET");
+ expect([PKTClient HTTPMethodForMethod:PKTRequestMethodPOST]).to.equal(@"POST");
+ expect([PKTClient HTTPMethodForMethod:PKTRequestMethodPUT]).to.equal(@"PUT");
+ expect([PKTClient HTTPMethodForMethod:PKTRequestMethodDELETE]).to.equal(@"DELETE");
+}
+
+- (void)testURLRequestForRequest {
+ NSString *path = @"/some/path";
+ PKTRequest *request = [PKTRequest GETRequestWithPath:path];
+
+ NSURLRequest *urlRequest = [self.testClient URLRequestForRequest:request];
+ expect(urlRequest.URL.path).to.equal(path);
+ expect(urlRequest.HTTPMethod).to.equal(@"GET");
+
+ // TODO: Check parameters
}
@end
@@ -0,0 +1,46 @@
+//
+// PKTRequestTests.m
+// PodioKit
+//
+// Created by Sebastian Rehnby on 21/01/14.
+// Copyright (c) 2014 Citrix Systems, Inc. All rights reserved.
+//
+
+#import <XCTest/XCTest.h>
+#import "PKTRequest.h"
+
+@interface PKTRequestTests : XCTestCase
+
+@end
+
+@implementation PKTRequestTests
+
+- (void)testGETRequest {
+ NSString *path = @"/some/path";
+ PKTRequest *request = [PKTRequest GETRequestWithPath:path];
+ expect(request.method).to.equal(PKTRequestMethodGET);
+ expect(request.path).to.equal(path);
+}
+
+- (void)testPOSTRequest {
+ NSString *path = @"/some/path";
+ PKTRequest *request = [PKTRequest POSTRequestWithPath:path];
+ expect(request.method).to.equal(PKTRequestMethodPOST);
+ expect(request.path).to.equal(path);
+}
+
+- (void)testPUTRequest {
+ NSString *path = @"/some/path";
+ PKTRequest *request = [PKTRequest PUTRequestWithPath:path];
+ expect(request.method).to.equal(PKTRequestMethodPUT);
+ expect(request.path).to.equal(path);
+}
+
+- (void)testDELETERequest {
+ NSString *path = @"/some/path";
+ PKTRequest *request = [PKTRequest DELETERequestWithPath:path];
+ expect(request.method).to.equal(PKTRequestMethodDELETE);
+ expect(request.path).to.equal(path);
+}
+
+@end

0 comments on commit b277f7e

Please sign in to comment.