Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored and implemented the other synchronous GET operations.

  • Loading branch information...
commit 9943172d3fc7b709f6d9b0190a790c020166acfa 1 parent 5fb943a
Luke Redpath authored
View
16 Classes/Categories/NSObject+SynchronousProxy.h
@@ -0,0 +1,16 @@
+//
+// NSObject+SynchronousProxy.h
+// LRResty
+//
+// Created by Luke Redpath on 20/01/2011.
+// Copyright 2011 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "LRSynchronousProxy.h"
+
+@interface NSObject (SynchronousProxy)
+
+- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+
+@end
View
19 Classes/Categories/NSObject+SynchronousProxy.m
@@ -0,0 +1,19 @@
+//
+// NSObject+SynchronousProxy.m
+// LRResty
+//
+// Created by Luke Redpath on 20/01/2011.
+// Copyright 2011 LJR Software Limited. All rights reserved.
+//
+
+#import "NSObject+SynchronousProxy.h"
+
+
+@implementation NSObject (SynchronousProxy)
+
+- (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+{
+ return [LRSynchronousProxy performAsynchronousBlockAndReturnResultWhenReady:block];
+}
+
+@end
View
31 Classes/LRRestyClient+GET.h
@@ -13,6 +13,9 @@
@interface LRRestyClient (GET)
+#pragma mark -
+#pragma mark Delegate API
+
/**
Performs a GET request on URL with delegate response handling.
@param urlString The URL to request.
@@ -40,6 +43,9 @@
*/
- (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers delegate:(id<LRRestyClientResponseDelegate>)delegate;
+#pragma mark -
+#pragma mark Blocks API
+
/**
Performs a GET request on URL with block response handling.
@param urlString The URL to request.
@@ -67,6 +73,9 @@
*/
- (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block;
+#pragma mark -
+#pragma mark Synchronous API
+
/**
Performs a *synchronous* GET request on URL, blocking the calling thread.
@param urlString The URL to request.
@@ -74,14 +83,32 @@
*/
- (LRRestyResponse *)get:(NSString *)urlString;
+/**
+ Performs a *synchronous* GET request on URL, blocking the calling thread.
+ @param urlString The URL to request.
+ @param parameters A dictionary of query string parameters.
+ @returns The response to the request.
+ */
+- (LRRestyResponse *)get:(NSString *)urlString parameters:(NSDictionary *)parameters;
+
+/**
+ Performs a *synchronous* GET request on URL, blocking the calling thread.
+ @param urlString The URL to request.
+ @param parameters A dictionary of query string parameters.
+ @param headers A dictionary of HTTP request headers.
+ @returns The response to the request.
+ */
+- (LRRestyResponse *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers;
+
@end
+#pragma mark -
+#pragma mark Streaming API
+
@interface LRRestyClient (GET_Streaming)
/**
Performs a GET request on URL, and yields data as it arrives. Designed for consuming streaming HTTP APIs.
@param urlString The URL to request.
- @param dataHandler Called with data as it receives.
- @param errorHandler Called when an error is encountered.
@returns The request object.
*/
- (LRRestyRequest *)get:(NSString *)urlString onData:(LRRestyStreamingDataBlock)dataHandler onError:(LRRestyStreamingErrorBlock)errorHandler;
View
37 Classes/LRRestyClient+GET.m
@@ -10,7 +10,7 @@
#import "LRRestyClientProxyDelegate.h"
#import "LRRestyClientBlockDelegate.h"
#import "LRRestyClientStreamingDelegate.h"
-#import "NSRunLoop+Additions.h"
+#import "NSObject+SynchronousProxy.h"
@implementation LRRestyClient (GET)
@@ -55,17 +55,32 @@ - (LRRestyRequest *)get:(NSString *)urlString parameters:(NSDictionary *)paramet
- (LRRestyResponse *)get:(NSString *)urlString;
{
- __block LRRestyResponse *response = nil;
-
- [self get:urlString withBlock:^(LRRestyResponse *theResponse) {
- response = [theResponse retain];
+ return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result)
+ {
+ [self get:urlString withBlock:^(LRRestyResponse *response) {
+ *result = [response retain];
+ }];
+ }];
+}
+
+- (LRRestyResponse *)get:(NSString *)urlString parameters:(NSDictionary *)parameters;
+{
+ return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result)
+ {
+ [self get:urlString parameters:parameters withBlock:^(LRRestyResponse *response) {
+ *result = [response retain];
+ }];
+ }];
+}
+
+- (LRRestyResponse *)get:(NSString *)urlString parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers;
+{
+ return [self performAsynchronousBlockAndReturnResultWhenReady:^(id *result)
+ {
+ [self get:urlString parameters:parameters headers:headers withBlock:^(LRRestyResponse *response) {
+ *result = [response retain];
+ }];
}];
-
- while (response == nil) {
- [[NSRunLoop currentRunLoop] runForTimeInterval:0.1];
- }
-
- return [response autorelease];
}
@end
View
17 Classes/LRSynchronousProxy.h
@@ -0,0 +1,17 @@
+//
+// LRSynchronousProxy.h
+// LRResty
+//
+// Created by Luke Redpath on 20/01/2011.
+// Copyright 2011 LJR Software Limited. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+typedef void (^LRSynchronousProxyBlock)(id *);
+
+@interface LRSynchronousProxy : NSObject {
+
+}
++ (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+@end
View
26 Classes/LRSynchronousProxy.m
@@ -0,0 +1,26 @@
+//
+// LRSynchronousProxy.m
+// LRResty
+//
+// Created by Luke Redpath on 20/01/2011.
+// Copyright 2011 LJR Software Limited. All rights reserved.
+//
+
+#import "LRSynchronousProxy.h"
+#import "NSRunLoop+Additions.h"
+
+@implementation LRSynchronousProxy
+
++ (id)performAsynchronousBlockAndReturnResultWhenReady:(LRSynchronousProxyBlock)block;
+{
+ id result = nil;
+
+ block(&result);
+
+ while (result == nil) {
+ [[NSRunLoop currentRunLoop] runForTimeInterval:0.1];
+ }
+ return [result autorelease];
+}
+
+@end
View
2  Gemfile.lock
@@ -1,6 +1,6 @@
GIT
remote: git@github.com:lukeredpath/mimic.git
- revision: 06fcb14a65119007fd10299fb821969f3a351bf4
+ revision: 60576b7a227e6c5f537bd8278a7c9baa3e0a3e1b
branch: develop
specs:
mimic (0.4.1)
View
32 LRResty.xcodeproj/project.pbxproj
@@ -40,6 +40,18 @@
A33E505212E89EB900746A2C /* NSRunLoop+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */; };
A33E505312E89EB900746A2C /* NSRunLoop+Additions.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E504D12E89EB900746A2C /* NSRunLoop+Additions.h */; };
A33E505412E89EB900746A2C /* NSRunLoop+Additions.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */; };
+ A33E50C512E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */; };
+ A33E50C612E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */; };
+ A33E50C712E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */; };
+ A33E50C812E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */; };
+ A33E50C912E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */; };
+ A33E50CA12E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */; };
+ A33E50CD12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */; };
+ A33E50CE12E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */; };
+ A33E50CF12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */; };
+ A33E50D012E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */; };
+ A33E50D112E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */; };
+ A33E50D212E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */; };
A35D8586125A83C200E5A8BD /* LRResty.h in Headers */ = {isa = PBXBuildFile; fileRef = A389DFFD125381700006572D /* LRResty.h */; settings = {ATTRIBUTES = (Public, ); }; };
A35D8587125A83C200E5A8BD /* LRRestyClient+GET.h in Headers */ = {isa = PBXBuildFile; fileRef = A389E001125381700006572D /* LRRestyClient+GET.h */; settings = {ATTRIBUTES = (Public, ); }; };
A35D8588125A83C200E5A8BD /* LRRestyClient+POST.h in Headers */ = {isa = PBXBuildFile; fileRef = A389E003125381700006572D /* LRRestyClient+POST.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -305,6 +317,10 @@
A332B0491229CEB900B8F201 /* libLRResty.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libLRResty.a; sourceTree = BUILT_PRODUCTS_DIR; };
A33E504D12E89EB900746A2C /* NSRunLoop+Additions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSRunLoop+Additions.h"; sourceTree = "<group>"; };
A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSRunLoop+Additions.m"; sourceTree = "<group>"; };
+ A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LRSynchronousProxy.h; sourceTree = "<group>"; };
+ A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LRSynchronousProxy.m; sourceTree = "<group>"; };
+ A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SynchronousProxy.h"; sourceTree = "<group>"; };
+ A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SynchronousProxy.m"; sourceTree = "<group>"; };
A35D8777125A8E3200E5A8BD /* LRRestyFramework-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "LRRestyFramework-Info.plist"; path = "Resources/LRRestyFramework-Info.plist"; sourceTree = "<group>"; };
A3702E101229E27000A2C962 /* Tests_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Tests_Prefix.pch; sourceTree = "<group>"; };
A3702EED1229E73D00A2C962 /* Compatibility Tests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Compatibility Tests.octest"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -746,6 +762,8 @@
A389DFB2125380080006572D /* NSURL+QueryInspector.m */,
A33E504D12E89EB900746A2C /* NSRunLoop+Additions.h */,
A33E504E12E89EB900746A2C /* NSRunLoop+Additions.m */,
+ A33E50CB12E8ACE800746A2C /* NSObject+SynchronousProxy.h */,
+ A33E50CC12E8ACE800746A2C /* NSObject+SynchronousProxy.m */,
);
path = Categories;
sourceTree = "<group>";
@@ -994,6 +1012,8 @@
children = (
A3FFFE5B125A5D380075D21F /* LRURLRequestOperation.h */,
A3FFFE5C125A5D380075D21F /* LRURLRequestOperation.m */,
+ A33E50C312E8AC6F00746A2C /* LRSynchronousProxy.h */,
+ A33E50C412E8AC6F00746A2C /* LRSynchronousProxy.m */,
);
name = Utility;
sourceTree = "<group>";
@@ -1009,6 +1029,8 @@
A332B0321229CEB900B8F201 /* RemoteRepositoryExample.h in Headers */,
A3702E111229E27000A2C962 /* Tests_Prefix.pch in Headers */,
A33E505112E89EB900746A2C /* NSRunLoop+Additions.h in Headers */,
+ A33E50C712E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */,
+ A33E50CF12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1044,6 +1066,8 @@
A3C630A312859826009A800C /* LRRestyResponse.h in Headers */,
A3C630A412859826009A800C /* LRRestyTypes.h in Headers */,
A33E505312E89EB900746A2C /* NSRunLoop+Additions.h in Headers */,
+ A33E50C912E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */,
+ A33E50D112E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1102,6 +1126,8 @@
A389E02E125381700006572D /* LRRestyResponse.h in Headers */,
A389E030125381700006572D /* LRRestyTypes.h in Headers */,
A33E504F12E89EB900746A2C /* NSRunLoop+Additions.h in Headers */,
+ A33E50C512E8AC6F00746A2C /* LRSynchronousProxy.h in Headers */,
+ A33E50CD12E8ACE800746A2C /* NSObject+SynchronousProxy.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1381,6 +1407,8 @@
buildActionMask = 2147483647;
files = (
A33E505212E89EB900746A2C /* NSRunLoop+Additions.m in Sources */,
+ A33E50C812E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */,
+ A33E50D012E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1453,6 +1481,8 @@
A3C630B912859826009A800C /* LRURLRequestOperation.m in Sources */,
A3C630BA12859826009A800C /* LRRestyClientProxyDelegate.m in Sources */,
A33E505412E89EB900746A2C /* NSRunLoop+Additions.m in Sources */,
+ A33E50CA12E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */,
+ A33E50D212E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1548,6 +1578,8 @@
A35D8750125A8D7A00E5A8BD /* LRURLRequestOperation.m in Sources */,
A35D8751125A8D8D00E5A8BD /* LRRestyClientProxyDelegate.m in Sources */,
A33E505012E89EB900746A2C /* NSRunLoop+Additions.m in Sources */,
+ A33E50C612E8AC6F00746A2C /* LRSynchronousProxy.m in Sources */,
+ A33E50CE12E8ACE800746A2C /* NSObject+SynchronousProxy.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
22 Tests/Acceptance/GetResourceTests.m
@@ -92,13 +92,25 @@ - (void)testCanPerformGetRequestAndPassResponseToABlock
- (void)testCanPerformSynchronousGetRequest
{
- __block LRRestyResponse *testLocalResponse = nil;
+ LRRestyResponse *response = [client get:resourceWithPath(@"/synchronous/echo")];
+ assertThat(response, is(responseWithStatus(200)));
+}
+
+- (void)testCanPerformSynchronousGetRequestWithParameters
+{
+ LRRestyResponse *response = [client get:resourceWithPath(@"/synchronous/echo")
+ parameters:[NSDictionary dictionaryWithObject:@"bar" forKey:@"foo"]];
- mimicGET(@"/simple/resource", andReturnAnything(), ^{
- testLocalResponse = [[client get:resourceWithPath(@"/simple/resource")] retain];
- });
+ assertThat(response, is(responseWithRequestEcho(@"params.foo", @"bar")));
+}
+
+- (void)testCanPerformSynchronousGetRequestWithCustomHeaders
+{
+ LRRestyResponse *response = [client get:resourceWithPath(@"/synchronous/echo")
+ parameters:nil
+ headers:[NSDictionary dictionaryWithObject:@"Resty" forKey:@"X-Test-Header"]];
- assertEventuallyThat(&testLocalResponse, is(responseWithStatus(200)));
+ assertThat(response, is(responseWithRequestEcho(@"env.HTTP_X_TEST_HEADER", @"Resty")));
}
#pragma mark -
View
7 Tests/Stubs/mimic.rb
@@ -68,3 +68,10 @@ def length
@string.split(" ").length + @string.length
end
end
+
+
+### Used by all synchronous tests
+
+get "/synchronous/echo" do
+ echo_request!(:plist)
+end
Please sign in to comment.
Something went wrong with that request. Please try again.