Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Source/Timberjack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ open class Timberjack: URLProtocol {
self.logHeaders(headers as [String : AnyObject])
}
}

if let body = request.body(),
let bodyString = String(data: body, encoding: .utf8) {
print("Body: \(bodyString)")
}
}

open func logResponse(_ response: URLResponse, data: Data? = nil) {
Expand Down Expand Up @@ -199,3 +204,39 @@ open class Timberjack: URLProtocol {
print("]")
}
}

fileprivate extension URLRequest {

func body() -> Data? {
var data: Data?

if let body = self.httpBody {
data = body
} else if let stream = self.httpBodyStream {
stream.open()
data = stream.readData()
stream.close()
}

return data
}
}

fileprivate extension InputStream {

func readData() -> Data {
let maxLength = 4096

var data = Data()

var buffer = Array<UInt8>(repeating: 0, count:maxLength)

var bytesRead = self.read(&buffer, maxLength: maxLength)
while bytesRead > 0 {
data.append(&buffer, count: bytesRead)
bytesRead = self.read(&buffer, maxLength: maxLength)
}

return data
}
}
2 changes: 1 addition & 1 deletion Tests/TimberjackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TimberjackTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
self.measure() {
// Put the code you want to measure the time of here.
}
}
Expand Down
3 changes: 3 additions & 0 deletions Timberjack.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
};
A1D265981B9999E3000D8735 = {
CreatedOnToolsVersion = 6.4;
LastSwiftMigration = 0830;
};
};
};
Expand Down Expand Up @@ -378,6 +379,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "co.rockettown.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -389,6 +391,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "co.rockettown.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down