Swiftpack.co - nearfri/HTTPMock as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by nearfri.
nearfri/HTTPMock 0.9.3
A Swift library for stubbing HTTP requests.
⭐️ 0
πŸ•“ 1 year ago
iOS macOS
.package(url: "https://github.com/nearfri/HTTPMock.git", from: "0.9.3")


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 생성 μ‹œ μžλ™μœΌλ‘œ λ“±λ‘ν•˜κ³  μ‹Άλ‹€λ©΄ μ•„λž˜μ˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

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

URLSession은 생성 ν›„ configuration 변경이 λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€. λ”°λΌμ„œ URLSession.sharedλ₯Ό μ‚¬μš©ν•œλ‹€λ©΄ μ•„λž˜μ˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•©λ‹ˆλ‹€.


등둝을 ν•΄μ œν•˜κ³  μ‹Άλ‹€λ©΄ μ•„λž˜μ˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

URLProtocolService.unregisterStub(from: configuration)

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)

μˆœμ„œ 상관없이 μœ„ 두 μž‘μ—…μ„ ν•œ ν›„ URLSession.dataTask(with:)λ‚˜ URLSession.downloadTask(with:)λ₯Ό ν˜ΈμΆœν•˜λ©΄ stub된 데이터λ₯Ό 얻을 수 μžˆμŠ΅λ‹ˆλ‹€.


Swift Package Manager

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

μ œν•œμ‚¬ν•­

  • background session은 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. (λΆˆκ°€λŠ₯)
  • μ—…λ‘œλ“œ μ—­μ‹œ μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. (λΆˆκ°€λŠ₯ν•œ κ²ƒμœΌλ‘œ μ•Œκ³  있음)


Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics