Skip to content

Commit

Permalink
Merge pull request #24 from venmo/daz/equalBody
Browse files Browse the repository at this point in the history
Compare encoded bodies instead of data when possible Fixes #23
  • Loading branch information
dasmer committed Aug 10, 2015
2 parents c5e5a6e + 7ffd66c commit f74e5fa
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions DVR/Cassette.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import Foundation

struct Cassette {

// MARK: - Properties

let name: String
let interactions: [Interaction]


// MARK: - Initializers

init(name: String, interactions: [Interaction]) {
self.name = name
self.interactions = interactions
}


// MARK: - Functions

func interactionForRequest(request: NSURLRequest) -> Interaction? {
for interaction in interactions {
let r = interaction.request
let interactionRequest = interaction.request

// Note: We don't check headers right now
if r.HTTPMethod == request.HTTPMethod && r.URL == request.URL && r.HTTPBody == request.HTTPBody {
if interactionRequest.HTTPMethod == request.HTTPMethod && interactionRequest.URL == request.URL && interactionRequest.hasHTTPBodyEqualToThatOfRequest(request) {
return interaction
}
}
Expand Down Expand Up @@ -43,3 +52,17 @@ extension Cassette {
}
}
}

private extension NSURLRequest {
func hasHTTPBodyEqualToThatOfRequest(request: NSURLRequest) -> Bool {
if let body1 = self.HTTPBody,
body2 = request.HTTPBody,
encoded1 = Interaction.encodeBody(body1, headers: self.allHTTPHeaderFields),
encoded2 = Interaction.encodeBody(body2, headers: request.allHTTPHeaderFields) {

return encoded1.isEqual(encoded2)
} else {
return self.HTTPBody == request.HTTPBody
}
}
}

0 comments on commit f74e5fa

Please sign in to comment.