Giter Site home page Giter Site logo

httpmock's Introduction

HTTPMock

Swift codecov

HTTPMock은 HTTP 호출 시 가짜 데이터를 다운로드받을 수 있도록 해주는 라이브러리입니다. 네트워크 호출이 필요한 앱을 서버 없이 유닛 테스트할 때 유용하게 쓸 수 있습니다.

사용법

URLSessionConfiguration에 custom URLProtocol 등록

URLSession 사용 시 stub된 데이터를 받을 수 있도록 하기 위해서는 URLSessionConfiguration.protocolClasses에 custom class를 등록해야 합니다. 등록하는 방법으로는 아래 세 가지가 있습니다.

URLSessionConfiguration에 직접 등록하는 방법입니다.

let configuration = URLSessionConfiguration.default
URLProtocolService.registerStub(in: configuration)
let session = URLSession(configuration: configuration)
...

URLSessionConfiguration 생성 시 자동으로 등록하고 싶다면 아래의 메소드를 사용합니다.

URLProtocolService.registerStubInDefaultConfiguration()
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration)
...

URLSession은 생성 후 configuration 변경이 불가능합니다. 따라서 URLSession.shared를 사용한다면 아래의 메소드를 호출해야 합니다.

URLProtocolService.registerStubInSharedSession()

등록을 해제하고 싶다면 아래의 메소드를 사용합니다.

URLProtocolService.unregisterStub(from: configuration)
URLProtocolService.unregisterStubFromDefaultConfiguration()
URLProtocolService.unregisterStubFromEphemeralConfiguration()
URLProtocolService.unregisterStubFromSharedSession()

Stub 할 request와 response 설정

let bundle = Bundle(for: type(of: self))
let responseFileURL = bundle.url(forResource: "items", withExtension: "json")!

stub(when: .isHost("server.com"), then: .fileURL(responseFileURL))

// stub 시 다양한 설정 가능. ex) 2초 딜레이 후 1KB씩 전송
stub(when: .isHost("server.com") && .hasLastPathComponent("items.json"),
     then: .fileURL(responseFileURL)
        .settingResponseDelay(2.0)
        .settingPreferredBytesPerSecond(1_000))

순서 상관없이 위 두 작업을 한 후 URLSession.dataTask(with:)URLSession.downloadTask(with:)를 호출하면 stub된 데이터를 얻을 수 있습니다.

설치

Swift Package Manager

.package(url: "https://github.com/nearfri/HTTPMock", from: "0.9.0")

제한사항

  • background session은 지원하지 않습니다. (불가능)
  • 업로드 역시 지원하지 않습니다. (불가능한 것으로 알고 있음)

httpmock's People

Contributors

nearfri avatar

Watchers

 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.