forked from alexrudall/ruby-openai
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds body and headers matching for VCR
- Loading branch information
1 parent
ba9a729
commit 937926f
Showing
2 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class VCRMultipartMatcher | ||
MULTIPART_HEADER_MATCHER = %r{^multipart/form-data; boundary=(.+)$}.freeze | ||
BOUNDARY_SUBSTITUTION = "----MultipartBoundaryAbcD3fGhiXyz00001".freeze | ||
|
||
def call(request1, request2) | ||
return false unless same_content_type?(request1, request2) | ||
unless headers_excluding_content_type(request1) == headers_excluding_content_type(request2) | ||
return false | ||
end | ||
|
||
normalized_multipart_body(request1) == normalized_multipart_body(request2) | ||
end | ||
|
||
private | ||
|
||
def same_content_type?(request1, request2) | ||
content_type1 = (request1.headers["Content-Type"] || []).first.to_s | ||
content_type2 = (request2.headers["Content-Type"] || []).first.to_s | ||
|
||
if multipart_request?(content_type1) | ||
multipart_request?(content_type2) | ||
elsif multipart_request?(content_type2) | ||
false | ||
else | ||
content_type1 == content_type2 | ||
end | ||
end | ||
|
||
def headers_excluding_content_type(request) | ||
request.headers.except("Content-Type") | ||
end | ||
|
||
def normalized_multipart_body(request) | ||
content_type = (request.headers["Content-Type"] || []).first.to_s | ||
|
||
return request.headers unless multipart_request?(content_type) | ||
|
||
boundary = MULTIPART_HEADER_MATCHER.match(content_type)[1] | ||
request.body.gsub(boundary, BOUNDARY_SUBSTITUTION) | ||
end | ||
|
||
def multipart_request?(content_type) | ||
return false if content_type.empty? | ||
|
||
MULTIPART_HEADER_MATCHER.match?(content_type) | ||
end | ||
end |