Coder Social home page Coder Social logo

urlmock's Introduction

URLMock

Build Status codecov

URLMock is an Objective-C framework for mocking and stubbing URL requests and responses. It works with APIs built on the Foundation NSURL loading system—NSURLConnection, NSURLSession, and AFNetworking, for example—with almost no changes to your code.

Features

  • Simple, well-documented Objective-C API
  • Minimal setup necessary
  • Works with APIs built atop the Foundation NSURL loading system
  • Designed with both response stubbing and unit testing in mind
  • Can be used for some or all of your project’s URL requests
  • Well-tested and includes lots of helpful testing utilities
  • Works on macOS, iOS, and tvOS

What’s New in URLMock 1.3.6

URLMock 1.3.6 adds support for installing using Swift Package Manager.

Installation

The easiest way to start using URLMock is to install it with CocoaPods.

pod 'URLMock'

Installing Subspecs

URLMock has two CocoaPods subspecs, TestHelpers and SubclassResponsibility. TestHelpers includes a wide variety of useful testing functions. See UMKTestUtilities.h for more details. It can be installed by adding the following line to your Podfile:

pod 'URLMock/TestHelpers'

Similarly, the SubclassResponsibility subspec can be installed by adding the following line to your Podfile:

pod 'URLMock/SubclassResponsibility'

This subspec adds methods to NSException to easily raise exceptions in methods for which subclasses must provide an implementation. See NSException+UMKSubclassResponsibility.h for details.

Using URLMock

URLMock is designed with both response stubbing and unit testing in mind. Both work very similarly.

Response stubbing

Using URLMock for response stubbing is simple:

First, enable URLMock.

[UMKMockURLProtocol enable];

If you are using NSURLSession and not using the shared session, you also need to add UMKMockURLProtocol to your session configuration’s set of allowed protocol classes.

NSURLSessionConfiguration *configuration = …;
configuration.protocolClasses = @[ [UMKMockURLProtocol class] ];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

Next, add an expected mock request and response.

// The request is a POST with some JSON data
NSURL *URL = [NSURL URLWithString:@"http://host.com/api/v1/person"];
id requestJSON = @{ @"person" : @{ @"name" : @"John Doe",
                                   @"age" : @47 } };
id responseJSON = @{ @"person" : @{ @"id" : @1,
                                    @"name" : @"John Doe",
                                    @"age" : @47 } };

[UMKMockURLProtocol expectMockHTTPPostRequestWithURL:URL
                                         requestJSON:requestJSON
                                  responseStatusCode:200
                                        responseJSON:responseJSON];

Mock requests and responses are not limited to having JSON bodies; they can also have bodies with strings, WWW form-encoded parameter dictionaries, or arbitrary NSData instances. There are also mock responders for responding with an error or returning data in chunks with a delay between each chunk, and we’ll be adding more responders in the future.

When you execute your real request, you will get the stubbed response back. You don’t have to make any changes to your code when using URLMock. Things should just work. For example, the following URLConnection code will receive the mock response above:

NSURL *URL = [NSURL URLWithString:@"http://host.com/api/v1/person"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
request.HTTPMethod = @"POST";
id bodyJSON = @{ @"person" : @{ @"name" : @"John Doe", @"age" : @47 } };
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:bodyJSON
                                                   options:0
                                                     error:NULL];

// Create the connection as usual
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:…];

The following AFNetworking code would accomplish the same thing:

NSURL *base = [NSURL URLWithString:@"http://host.com/api/v1/"];
id params = @{ @"person" : @{ @"name" : @"John Doe", @"age" : @47 } };

// Send a POST as usual
AFHTTPRequestOperationManager *om = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:base];
om.requestSerializer = [AFJSONRequestSerializer serializer];
[om POST:@"person" parameters:params success:^(AFHTTPRequestOperation *op, id object) {
    …
} failure:^(AFHTTPRequestOperation *op, NSError *error) {
    …
}];

Pattern-Matching Mock Requests

You can also create a mock request that responds dynamically to the request it matches against using UMKPatternMatchingMockRequest. To create a pattern-matching mock request, you need to provide a URL pattern, e.g., @"http://hostname.com/:resource/:resourceID". When a URL request matches this pattern, the mock request generates an appropriate responder using its responder generation block:

NSString *pattern = @"http://hostname.com/accounts/:accountID/followers";
UMKPatternMatchingMockRequest *mockRequest =  [[UMKPatternMatchingMockRequest alloc] initWithPattern:pattern];
mockRequest.HTTPMethods = [NSSet setWithObject:kUMKMockHTTPRequestPostMethod];

mockRequest.responderGenerationBlock = ^id<UMKMockURLResponder>(NSURLRequest *request, NSDictionary *parameters) {
    NSDictionary *requestJSON = [request umk_JSONObjectFromHTTPBody];

    // Respond with
    //   {
    //     "follower_id": «New follower’s ID»,
    //     "following_id":  «Account ID that was POSTed to»
    //   }
    UMKMockHTTPResponder *responder = [UMKMockHTTPResponder mockHTTPResponderWithStatusCode:200];
    [responder setBodyWithJSONObject:@{ @"follower_id" : requestJSON[@"follower_id"],
                                        @"following_id" : @([parameters[@"accountID"] integerValue]) }];
    return responder;
};

[UMKMockURLProtocol addExpectedMockRequest:mockRequest];

See the documentation for UMKPatternMatchingMockRequest for more information.

Unit testing

Unit testing with URLMock is very similar to response stubbing, but you can use a few additional APIs to make unit testing easier.

First, enable verification in UMKMockURLProtocol using +setVerificationEnabled:. This enables tracking whether any unexpected requests were received. It makes sense to do this in your XCTestCase’s +setUp method. You can also disable verification in your +tearDown method.

+ (void)setUp
{
    [super setUp];
    [UMKMockURLProtocol enable];
    [UMKMockURLProtocol setVerificationEnabled:YES];
}


+ (void)tearDown
{
    [UMKMockURLProtocol setVerificationEnabled:NO];
    [UMKMockURLProtocol disable];
    [super tearDown];
}

Before (or after) each test, invoke +[UMKMockURLProtocol reset]. This resets UMKMockURLProtocol’s expectations to their original state. It does not change whether verification is enabled.

If you’re using XCTest, the ideal place to do this is in your test case’s ‑setUp (or ‑tearDown) method.

- (void)setUp
{
   [super setUp];
   [UMKMockURLProtocol reset];
}

After you’ve executed the code you’re testing, send UMKMockURLProtocol the +verifyWithError: message. It will return YES if all expected mock requests were serviced and no unexpected mock requests were received.

NSError *error = nil;
XCTAssertTrue([UMKMockURLProtocol verifyWithError:&error], @"…");

For the strictest testing, enable header checking on your UMKMockHTTPRequest instances. When enabled, mock requests only match URL requests that have equivalent headers. You can enable header checking on mock HTTP request by setting its checksHeadersWhenMatching property to YES or by using ‑initWithHTTPMethod:URL:checksHeadersWhenMatching:.

UMKMockHTTPRequest *request = [UMKMockHTTPRequest mockHTTPGetRequestWithURL:URL];
request.checksHeadersWhenMatching = YES;

Note that some networking APIs—most notably AFNetworking—send headers that you didn’t explicitly set, so you should determine what those are before creating your mock requests. To make things a little easier, you can use +[UMKMockHTTPRequest setDefaultHeaders:] to set the default headers for new UMKMockHTTPRequest instances. For example, if you’re using AFNetworking’s default HTTP request serializer, you can set default headers this way:

[UMKMockHTTPRequest setDefaultHeaders:[[AFHTTPRequestSerializer serializer] HTTPRequestHeaders]];

Non-HTTP Protocols

Out of the box, URLMock only supports HTTP and HTTPS. However, it is designed to work with any URL protocol that the NSURL loading system can support. If you are using a custom scheme or URL protocol and would like to add support for mocking requests and responses, you need only create classes that conform to the UMKMockURLRequest and UMKMockURLResponder protocols. See the implementations of UMKMockHTTPRequest and UMKMockHTTPResponder for examples.

Owners

@jnjosh, @prachigauriar, @macdrevx, and @dfowj currently act as the owners of URLMock. Mention us in issues or pull requests for questions about features, project direction, or to request code review.

Contributing, Filing Bugs, and Requesting Enhancements

URLMock is very usable in its current state, but there’s still a lot that could be done. If you would like to help fix bugs or add features, send us a pull request!

We use GitHub issues for bugs, enhancement requests, and the limited support we provide, so open an issue for any of those.

Typically, a pull request should receive a code review and a 👍 from at least 2 project owners before being merged. In cases where a pull request review needs to be expedited a single 👍 from an owner will suffice, though this should be the exception, not the rule.

License

All code is licensed under the MIT license. Do with it as you will.

urlmock's People

Contributors

danielrhammond avatar flightblog avatar igorpakushin avatar jnjosh avatar prachigauriar avatar slavikus avatar snf-tms avatar tomburns avatar z3bi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

urlmock's Issues

expectMockHTTPPostRequestWithURL does not set content type

    [UMKMockURLProtocol expectMockHTTPPostRequestWithURL:URL requestJSON:requestJSON responseStatusCode:200 responseJSON:responseJSON];

If I am setting JSON as POST body and expecting JSON response should the framework also set the content-type header to 'application/json'?

Simple cut and paste from your example does not work as the content-type is not set correctly. Should the content-type be set automatically when I say that I posting JSON and expecting JSON back?

- (BOOL)bodyMatchesBodyOfURLRequest:(NSURLRequest *)request

That is the method that fails to match the body in the case of content-type 'application/json
not being set.

Thoughts?

Library still isn't called HolyMockURL

Summary:

This library is pretty cool and all, but it needs a name that is just as cool.

Steps to Reproduce:

  1. Look at library
  2. See that its name is somewhat lacking

Expected Results:

Expected this to have a badass name like HolyMockURL

Actual Results:

Library is named URLMock, which is totes boring.

Version:

1.0.1

Really just 7.0?

When I try to install I get:
(iOS 6.0) is not compatible with `URLMock (1.1.1)

Is this really only compatible with 7.0+

Install Without CocoaPods

We have a large and complex project that contains multiple sets of xcconfig files which include each other to form a hierarchy. Xcode does not support inheriting values in xcconfig files like it does in the IDE. We cannot use CocoaPods in our project.

After trying to include your project as a sub-project in our own, I noticed that the directories and file structures do not seem to be in the final order. For example: URLMock.h contains the line #import <URLMock/UMKMockURLProtocol.h> but in the project's directory structure the file is actually at URLMock/Mock URL Protocol/UMKMockURLProtocol.h.

This makes it non-trivial to use URLMock without using CocoaPods.

Request hangs because URLMock doesn't read the HTTPBodyStream

Firstly, great work with this library! I heard about NSURLProtocol on Saul Mora's podcast, and immediately decided to make a 'MXMockURLProtocol'. Later I finished the podcast and discovered this library, which is exactly what I hoped to build!

I am having trouble with one of my requests, though. It's an upload of a file to Amazon S3 using AFHTTPRequestSerializer, it looks like this:

(lldb) po self.request
<NSURLRequest: 0x6180000045d0> { URL: https://mixim-unittest.s3.amazonaws.com/ }
(lldb) po [self.request allHTTPHeaderFields]
{
    "Accept-Language" = "en-GB;q=1, es;q=0.9, en;q=0.8, fr;q=0.7, de;q=0.6, zh-Hans;q=0.5";
    "Content-Length" = 411;
    "Content-Type" = "multipart/form-data; boundary=Boundary+1F6B675C1E398953";
    Expect = "100-continue";
    "User-Agent" = "Mixim/alpha6rc1-5-g006d85a-dirty (Mac OS X Version 10.9.4 (Build 13E28))";
}
(lldb) po [self.request HTTPBody]
 nil
(lldb) po [self.request HTTPBodyStream]
<AFMultipartBodyStream: 0x61800014aea0>

The request never completes. I think this is because the HTTP client is waiting for the body stream to be exhausted, because I subclassed UMKURLProtocol to add this:

- (void)startLoading
{
    // this is a patch for the mock URL protocol to prevent the client from hanging, waiting for the stream to be read
    NSInputStream *stream = [self.request HTTPBodyStream];

    if (stream) {
        uint8_t fakeBuffer[1024];

        while([stream read:fakeBuffer maxLength:1024] == 1024) {};
    }

    [super startLoading];
}

and that seemed to fix the hanging issue. But I suspect this is the wrong place to put this code!

Cannot find support for XML encoding?

I can see following supported:
NSString *const kUMKMockHTTPMessageJSONContentTypeHeaderValue = @"application/json";
NSString *const kUMKMockHTTPMessageUTF8JSONContentTypeHeaderValue = @"application/json; charset=utf-8";
NSString *const kUMKMockHTTPMessageWWWFormURLEncodedContentTypeHeaderValue = @"application/x-www-form-urlencoded";
NSString *const kUMKMockHTTPMessageUTF8WWWFormURLEncodedContentTypeHeaderValue = @"application/x-www-form-urlencoded; charset=utf-8";

Add support for stream-based requests.

URLMock doesn’t work with stream-based requests, which means it’s not compatible with NSURLSession and is more difficult to use when memory is constrained.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.