From 4f7c1c68e08f308a65a62134c3f862ea1680e4f6 Mon Sep 17 00:00:00 2001 From: Christopher Pickslay Date: Tue, 17 Apr 2012 22:05:27 -0700 Subject: [PATCH] added support for payload in DELETE requests --- Classes/LRRestyClient+DELETE.h | 10 ++++++++++ Classes/LRRestyClient+DELETE.m | 9 +++++++-- Classes/LRRestyHTTPClient.h | 2 +- Classes/LRRestyHTTPClient.m | 4 ++-- Tests/Acceptance/DeleteResourceTests.m | 17 +++++++++++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/Classes/LRRestyClient+DELETE.h b/Classes/LRRestyClient+DELETE.h index 66e16fe..3adfd60 100644 --- a/Classes/LRRestyClient+DELETE.h +++ b/Classes/LRRestyClient+DELETE.h @@ -57,6 +57,16 @@ */ - (LRRestyRequest *)delete:(NSString *)urlString headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block; +/** + Performs a DELETE request on URL resource with block response handling. + @param urlString The URL resource to delete. + @param payload The HTTP request body + @param headers A dictionary of HTTP request headers. + @param block The response handler. + @returns The request object. + */ +- (LRRestyRequest *)delete:(NSString *)urlString payload:(id)payload headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block; + #pragma mark - #pragma mark Synchronous API diff --git a/Classes/LRRestyClient+DELETE.m b/Classes/LRRestyClient+DELETE.m index 9e4176e..9234792 100644 --- a/Classes/LRRestyClient+DELETE.m +++ b/Classes/LRRestyClient+DELETE.m @@ -24,7 +24,7 @@ - (LRRestyRequest *)delete:(NSString *)urlString delegate:(id)delegate; { - return [HTTPClient DELETE:[NSURL URLWithString:urlString] headers:headers delegate:[LRRestyClientProxyDelegate proxyForClient:self responseDelegate:delegate]]; + return [HTTPClient DELETE:[NSURL URLWithString:urlString] payload:nil headers:headers delegate:[LRRestyClientProxyDelegate proxyForClient:self responseDelegate:delegate]]; } #pragma mark - @@ -37,7 +37,12 @@ - (LRRestyRequest *)delete:(NSString *)urlString withBlock:(LRRestyResponseBlock - (LRRestyRequest *)delete:(NSString *)urlString headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block; { - return [HTTPClient DELETE:[NSURL URLWithString:urlString] headers:headers delegate:[LRRestyClientBlockDelegate delegateWithBlock:block]]; + return [HTTPClient DELETE:[NSURL URLWithString:urlString] payload:nil headers:headers delegate:[LRRestyClientBlockDelegate delegateWithBlock:block]]; +} + +- (LRRestyRequest *)delete:(NSString *)urlString payload:(id)payload headers:(NSDictionary *)headers withBlock:(LRRestyResponseBlock)block +{ + return [HTTPClient DELETE:[NSURL URLWithString:urlString] payload:payload headers:headers delegate:[LRRestyClientBlockDelegate delegateWithBlock:block]]; } #pragma mark - diff --git a/Classes/LRRestyHTTPClient.h b/Classes/LRRestyHTTPClient.h index 77571f1..a1e62cd 100644 --- a/Classes/LRRestyHTTPClient.h +++ b/Classes/LRRestyHTTPClient.h @@ -18,7 +18,7 @@ - (LRRestyRequest *)GET:(NSURL *)url parameters:(NSDictionary *)parameters headers:(NSDictionary *)headers delegate:(id)requestDelegate; - (LRRestyRequest *)POST:(NSURL *)url payload:(id)payload headers:(NSDictionary *)headers delegate:(id)requestDelegate; - (LRRestyRequest *)PUT:(NSURL *)url payload:(id)payload headers:(NSDictionary *)headers delegate:(id)requestDelegate; -- (LRRestyRequest *)DELETE:(NSURL *)url headers:(NSDictionary *)headers delegate:(id)requestDelegate; +- (LRRestyRequest *)DELETE:(NSURL *)url payload:(id)payload headers:(NSDictionary *)headers delegate:(id)requestDelegate; - (LRRestyRequest *)performRequest:(LRRestyRequest *)request; - (void)cancelAllRequests; @end diff --git a/Classes/LRRestyHTTPClient.m b/Classes/LRRestyHTTPClient.m index c2d3c4c..6004e49 100644 --- a/Classes/LRRestyHTTPClient.m +++ b/Classes/LRRestyHTTPClient.m @@ -62,9 +62,9 @@ - (LRRestyRequest *)PUT:(NSURL *)url payload:(id)payload headers:(NSDictionary * return [self performRequest:[self requestForURL:url method:@"PUT" payload:payload headers:headers requestDelegate:requestDelegate]]; } -- (LRRestyRequest *)DELETE:(NSURL *)url headers:(NSDictionary *)headers delegate:(id)requestDelegate; +- (LRRestyRequest *)DELETE:(NSURL *)url payload:(id)payload headers:(NSDictionary *)headers delegate:(id)requestDelegate; { - return [self performRequest:[self requestForURL:url method:@"DELETE" payload:nil headers:headers requestDelegate:requestDelegate]]; + return [self performRequest:[self requestForURL:url method:@"DELETE" payload:payload headers:headers requestDelegate:requestDelegate]]; } - (LRRestyRequest *)performRequest:(LRRestyRequest *)request; diff --git a/Tests/Acceptance/DeleteResourceTests.m b/Tests/Acceptance/DeleteResourceTests.m index cfddc50..7a41812 100644 --- a/Tests/Acceptance/DeleteResourceTests.m +++ b/Tests/Acceptance/DeleteResourceTests.m @@ -39,6 +39,23 @@ - (void)testCanPerformDeleteRequestWithCustomHeaders assertEventuallyThat(&lastResponse, is(responseWithRequestEcho(@"env.HTTP_X_TEST_HEADER", @"Resty"))); } +- (void)testCanPerformDeleteRequestWithHTTPBody +{ + __block LRRestyResponse *testLocalResponse = nil; + NSString *payload = @"{\"foo\":\"bar\"}"; + mimicDELETE(@"/simple/resource", andEchoRequest(), ^{ + [client delete:resourceWithPath(@"/simple/resource") + payload:payload + headers:[NSDictionary dictionaryWithObject:@"Resty" forKey:@"X-Test-Header"] + withBlock:^(LRRestyResponse *response) { + testLocalResponse = [response retain]; + }]; + }); + + assertEventuallyThat(&lastResponse, is(responseWithRequestEcho(@"body", payload))); + [testLocalResponse release]; +} + - (void)testCanModifyRequestsBeforeTheyAreSentUsingBlock { mimicDELETE(@"/simple/resource", andEchoRequest(), ^{