Swiftpack.co -  bannzai/XChanger as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
bannzai/XChanger
XChanger can exchange to behavior for URL request and response
.package(url: "https://github.com/bannzai/XChanger.git", from: "1.0.1")

XChanger

XChange makes us enable to do mocking response with URL easily.

Usage

Before start to register mocks, you need to call just one sentence, XChanger.register(), in some starting points like @main, AppDelegate, SceneDelegate or head of method in your unit test cases.

XChanger.register()

Then, passing response mock data with URL request as a key. Here is the example for the URL https://exmaple.com/v1/users/10, and will return the 200 response with the body {"id": 10, name:"bannzai"}.

struct User: Codable {
  var id: Int
  var name: String
}

let url = "https://exmaple.com/v1/users/10"
let json = try! JSONEncoder().encode(User(id: 10, name: "bannzai"))
XChanger.exchange().request(url: url).response(data: json, statusCode: 200).enable()

Finally, you can send URLRequest normaly, and you can see the response is what you defined.

let request = URLRequest(url: URL(string: "https://exmaple.com/v1/users/10")!)
let session = URLSession(configuration: URLSessionConfiguration.default)
session.dataTask(with: request) { data, response, error in
  guard let httpResponse = response as? HTTPURLResponse else {
      return fatalError("Unexpected response type of HTTPURLResponse")
  }
  print(httpResponse.statusCode) // 200
  
  let decoded = try! JSONDecoder().decode(User.self, from: data!)
  print(decoded) // User(id: 10, name: "bannzai")
}.resume()

Installing

Cocoapods

pod 'XChanger', configuration: %w(Debug)

Swift Package Manager

Xcode

Open File > Swift Packages > Add Package Dependency... and put Repository URL https://github.com/bannzai/XChanger. This document will also help you.

Use as dependency

Add the following to your Package.swift file's dependencies:

.package(url: "https://github.com/bannzai/XChanger.git", from: "0.0.1")

LICENSE

XChanger is released under the MIT license. See LICENSE for details.

GitHub

link
Stars: 29
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

43 weeks ago

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