Skip to content

Commit

Permalink
Add better debug descriptions for request/response objects
Browse files Browse the repository at this point in the history
  • Loading branch information
pwalters committed Sep 18, 2019
1 parent 7e57d33 commit a4233e2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 5 additions & 1 deletion MockDuck/Sources/MockRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

/// A very basic wrapper around URLRequest that allows us to read and write this data to disk
/// using Codable without having to make URLRequest itself conform to Codable.
final class MockRequest {
final class MockRequest: CustomDebugStringConvertible {
var request: URLRequest

private(set) lazy var normalizedRequest: URLRequest = {
Expand All @@ -20,6 +20,10 @@ final class MockRequest {
init(request: URLRequest) {
self.request = request
}

public var debugDescription: String {
return "\(request.httpMethod ?? "GET") \(request.url?.absoluteString ?? "")"
}
}

extension MockRequest: Codable {
Expand Down
25 changes: 24 additions & 1 deletion MockDuck/Sources/MockRequestResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// A basic container for holding a request, a response, and any associated data.
final class MockRequestResponse: Codable {
final class MockRequestResponse: Codable, CustomDebugStringConvertible {

enum MockFileTarget {
case request
Expand Down Expand Up @@ -141,4 +141,27 @@ final class MockRequestResponse: Codable {
requestWrapper = try container.decode(MockRequest.self, forKey: .requestWrapper)
responseWrapper = try container.decodeIfPresent(MockResponse.self, forKey: .responseWrapper)
}

// MARK: Debug

public var debugDescription: String {
var result = "\n"

if let request = fileName(for: .request) {
result.append("Request: \(request)\n")
result.append("\t\(requestWrapper)\n")
}
if let requestBody = fileName(for: .requestBody) {
result.append("Request Body: \(requestBody)\n")
}
if let responseData = fileName(for: .responseData) {
result.append("Response Data: \(responseData)\n")

if let response = responseWrapper {
result.append("\t\(response)\n")
}
}

return result
}
}
13 changes: 12 additions & 1 deletion MockDuck/Sources/MockResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

/// A basic class that holds onto a URLResponse and its associated data.
public final class MockResponse {
public final class MockResponse: CustomDebugStringConvertible {

var response: URLResponse
var responseData: Data?
Expand Down Expand Up @@ -82,6 +82,17 @@ public final class MockResponse {
let data = try JSONSerialization.data(withJSONObject: json, options: [])
try self.init(for: request, data: data, statusCode: statusCode, headers: headers)
}

public var debugDescription: String {
var result = ""
if let httpResponse = response as? HTTPURLResponse
{
result.append("\(httpResponse.statusCode) ")
}
result.append("\(response.url?.absoluteString ?? "")")
result.append(" (\(response.expectedContentLength) bytes)")
return result
}
}

// MARK: - Codable
Expand Down

0 comments on commit a4233e2

Please sign in to comment.