Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefactoring: Add broken test case for parsing multipart content type (#782) #815

Merged
merged 3 commits into from
Mar 25, 2022
Merged
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
44 changes: 40 additions & 4 deletions test/parsemultipart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ using HTTP
import HTTP.MultiPartParsing: find_multipart_boundary, find_multipart_boundaries, find_header_boundary, parse_multipart_chunk, parse_multipart_body, parse_multipart_form

function generate_test_body()
Vector{UInt8}("----------------------------918073721150061572809433\r\nContent-Disposition: form-data; name=\"namevalue\"; filename=\"multipart.txt\"\r\nContent-Type: text/plain\r\n\r\nnot much to say\n\r\n----------------------------918073721150061572809433\r\nContent-Disposition: form-data; name=\"key1\"\r\n\r\n1\r\n----------------------------918073721150061572809433\r\nContent-Disposition: form-data; name=\"key2\"\r\n\r\nkey the second\r\n----------------------------918073721150061572809433\r\nContent-Disposition: form-data; name=\"namevalue2\"; filename=\"multipart-leading-newline.txt\"\r\nContent-Type: text/plain\r\n\r\n\nfile with leading newline\n\r\n----------------------------918073721150061572809433--\r\n")
Vector{UInt8}(join([
"----------------------------918073721150061572809433",
"Content-Disposition: form-data; name=\"namevalue\"; filename=\"multipart.txt\"",
"Content-Type: text/plain",
"",
"not much to say\n",
"----------------------------918073721150061572809433",
"Content-Disposition: form-data; name=\"key1\"",
"",
"1",
"----------------------------918073721150061572809433",
"Content-Disposition: form-data; name=\"key2\"",
"",
"key the second",
"----------------------------918073721150061572809433",
"Content-Disposition: form-data; name=\"namevalue2\"; filename=\"multipart-leading-newline.txt\"",
"Content-Type: text/plain",
"",
"\nfile with leading newline\n",
"----------------------------918073721150061572809433",
"Content-Disposition: form-data; name=\"json_file1\"; filename=\"my-json-file-1.json\"",
"Content-Type: application/json",
"",
"{\"data\": [\"this is json data\"]}",
"----------------------------918073721150061572809433--",
"",
], "\r\n"))
end

function generate_test_request()
Expand Down Expand Up @@ -74,18 +100,23 @@ end
@test (startIndex + endIndexOffset) == endIndex

(isTerminatingDelimiter, startIndex, endIndex) = find_multipart_boundary(body, delimiter, start = startIndex + 3)
@test isTerminatingDelimiter
@test !isTerminatingDelimiter
@test 600 == startIndex
@test (startIndex + endIndexOffset) == endIndex

(isTerminatingDelimiter, startIndex, endIndex) = find_multipart_boundary(body, delimiter, start = startIndex + 3)
@test isTerminatingDelimiter
@test 804 == startIndex
# +2 because of the two additional '--' characters
@test (startIndex + endIndexOffset + 2) == endIndex
end


@testset "parse_multipart_form" begin
@test HTTP.parse_multipart_form(generate_non_multi_test_request()) === nothing

multiparts = HTTP.parse_multipart_form(generate_test_request())
@test 4 == length(multiparts)
@test 5 == length(multiparts)

@test "multipart.txt" === multiparts[1].filename
@test "namevalue" === multiparts[1].name
Expand All @@ -106,5 +137,10 @@ end
@test "namevalue2" === multiparts[4].name
@test "text/plain" === multiparts[4].contenttype
@test "\nfile with leading newline\n" === String(read(multiparts[4].data))

@test "my-json-file-1.json" === multiparts[5].filename
@test "json_file1" === multiparts[5].name
@test_broken "application/json" === multiparts[5].contenttype
@test """{"data": ["this is json data"]}""" === String(read(multiparts[5].data))
end
end