|
| 1 | +class VCRMultipartMatcher |
| 2 | + MULTIPART_HEADER_MATCHER = %r{^multipart/form-data; boundary=(.+)$}.freeze |
| 3 | + BOUNDARY_SUBSTITUTION = "----MultipartBoundaryAbcD3fGhiXyz00001".freeze |
| 4 | + |
| 5 | + def call(request1, request2) |
| 6 | + return false unless same_content_type?(request1, request2) |
| 7 | + unless headers_excluding_content_type(request1) == headers_excluding_content_type(request2) |
| 8 | + return false |
| 9 | + end |
| 10 | + |
| 11 | + normalized_multipart_body(request1) == normalized_multipart_body(request2) |
| 12 | + end |
| 13 | + |
| 14 | + private |
| 15 | + |
| 16 | + def same_content_type?(request1, request2) |
| 17 | + content_type1 = (request1.headers["Content-Type"] || []).first.to_s |
| 18 | + content_type2 = (request2.headers["Content-Type"] || []).first.to_s |
| 19 | + |
| 20 | + if multipart_request?(content_type1) |
| 21 | + multipart_request?(content_type2) |
| 22 | + elsif multipart_request?(content_type2) |
| 23 | + false |
| 24 | + else |
| 25 | + content_type1 == content_type2 |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def headers_excluding_content_type(request) |
| 30 | + request.headers.except("Content-Type") |
| 31 | + end |
| 32 | + |
| 33 | + def normalized_multipart_body(request) |
| 34 | + content_type = (request.headers["Content-Type"] || []).first.to_s |
| 35 | + |
| 36 | + return request.headers unless multipart_request?(content_type) |
| 37 | + |
| 38 | + boundary = MULTIPART_HEADER_MATCHER.match(content_type)[1] |
| 39 | + request.body.gsub(boundary, BOUNDARY_SUBSTITUTION) |
| 40 | + end |
| 41 | + |
| 42 | + def multipart_request?(content_type) |
| 43 | + return false if content_type.empty? |
| 44 | + |
| 45 | + MULTIPART_HEADER_MATCHER.match?(content_type) |
| 46 | + end |
| 47 | +end |
0 commit comments