Giter Club home page Giter Club logo

vokmockurlprotocol's Introduction

VOKMockUrlProtocol

CI Status Version License Platform

A url protocol that parses and returns fake responses with mock data.

Usage

Create a folder reference called VOKMockData, so that the entire VOKMockData directory is copied into your test app bundle and place mock data files in there. The easiest way to determine the proper file name for a mock data item is to make the mock API call and note the missing mock data file reported in the logs. The full naming convention is described here.

Note: The naming convention for mock data files has changed with version 2.x. Mock data files from version 1.x may need to be renamed to work with version 2.x, particularly for requests with a body (e.g., POST requests), particularly when the body is not application/x-www-form-urlencoded .

Mock data files may:

  • have the .json extension to always return an HTTP/1.1 200 Success with Content-type: text/json and the content of the .json file; or
  • have the .http extension to parse an HTTP response with the following format:
    • status on the first line
    • headers on the following lines
    • blank line
    • body on the following lines

Example HTTP responses:

HTTP response with headers and response body.

HTTP/1.1 201 CREATED
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 02 Oct 2014 20:50:29 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS

{"id": 63, "auth_token": "50db3356e743fa3f1b790a8648fc15cc4bbf04a2", "phone_number": "+13125551214", "email": "[email protected]", "name": "Testy McTesterson", "role": "Customer"}

HTTP response with headers and no body. Note the blank line at the end!

HTTP/1.1 202 Accepted
Content-Type: text/plain; charset=UTF-8
Date: Fri, 17 Oct 2014 14:12:46 GMT
Server: Apache-Coyote/1.1
Content-Length: 0
Connection: keep-alive

HTTP response with no headers and no body. Note the blank line at the end!

HTTP/1.1 202 Accepted

HTTP response with no headers and a body.

HTTP/1.1 202 Accepted

{"favorite_dog_breed": "dogfish"}

Using with NSURLSessionConfiguration

In order to get NSURLSesssion to use VOKMockUrlProtocol, you must insert its class into a NSURLSessionConfigurations's protocolClasses.

Example:

    Class mockURLProtocol = [VOKMockUrlProtocol class];
    NSMutableArray *currentProtocolClasses = [self.sessionConfiguration.protocolClasses mutableCopy];
    [currentProtocolClasses insertObject:mockURLProtocol atIndex:0];
    self.sessionConfiguration.protocolClasses = currentProtocolClasses;

In order to switch back and forth between mock and live, you can also take out the Mock Url Protocol by just removing it:

    Class mockURLProtocol = [VOKMockUrlProtocol class];
    NSMutableArray *currentProtocolClasses = [self.sessionConfiguration.protocolClasses mutableCopy];
    [currentProtocolClasses removeObject:mockURLProtocol];
    self.sessionConfiguration.protocolClasses = currentProtocolClasses;

vokmockurlprotocol's People

Contributors

vokal-isaac avatar chillpop avatar designatednerd avatar ilg avatar brockboland avatar jacobvanorder avatar

Watchers

James Cloos avatar Erik Krag avatar

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.