This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
/
MultipartMarshallersSpec.scala
147 lines (134 loc) · 5.67 KB
/
MultipartMarshallersSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (C) 2011-2013 spray.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spray.httpx.marshalling
import java.util.Random
import scala.collection.immutable.ListMap
import org.specs2.mutable.Specification
import spray.util._
import spray.http._
import MediaTypes._
import HttpCharsets._
import HttpHeaders._
class MultipartMarshallersSpec extends Specification with MultipartMarshallers {
protected class FixedRandom extends Random {
override def nextBytes(array: Array[Byte]): Unit = "my-stable-boundary".getBytes("UTF-8").copyToArray(array)
}
override protected val multipartBoundaryRandom = new FixedRandom // fix for stable value
sequential // required for stable random sequence
"The MultipartContentMarshaller" should {
"correctly marshal to multipart content with one empty part" in {
marshal(MultipartContent(Seq(BodyPart("")))) === Right {
HttpEntity(
contentType = ContentType(`multipart/mixed` withBoundary randomBoundary),
string = s"""|--${randomBoundary}
|--${randomBoundary}--""".stripMargin.replace(EOL, "\r\n"))
}
}
"correctly marshal multipart content with one part" in {
marshal {
MultipartContent {
Seq(
BodyPart(
entity = HttpEntity(ContentType(`text/plain`, `UTF-8`), "test@there.com"),
headers = `Content-Disposition`("form-data", Map("name" -> "email")) :: Nil))
}
} === Right {
HttpEntity(ContentType(`multipart/mixed` withBoundary randomBoundary),
s"""|--${randomBoundary}
|Content-Disposition: form-data; name=email
|Content-Type: text/plain; charset=UTF-8
|
|test@there.com
|--${randomBoundary}--""".stripMargin.replace(EOL, "\r\n"))
}
}
"correctly marshal multipart content with two different parts" in {
marshal {
MultipartContent {
Seq(
BodyPart(HttpEntity(ContentType(`text/plain`, Some(`US-ASCII`)), "first part, with a trailing linebreak\r\n")),
BodyPart(
HttpEntity(ContentType(`application/octet-stream`), "filecontent"),
RawHeader("Content-Transfer-Encoding", "binary") :: Nil))
}
} === Right {
HttpEntity(ContentType(`multipart/mixed` withBoundary randomBoundary),
s"""|--${randomBoundary}
|Content-Type: text/plain; charset=US-ASCII
|
|first part, with a trailing linebreak
|
|--${randomBoundary}
|Content-Transfer-Encoding: binary
|Content-Type: application/octet-stream
|
|filecontent
|--${randomBoundary}--""".stripMargin.replace(EOL, "\r\n"))
}
}
}
"The MultipartFormDataMarshaller" should {
"correctly marshal 'multipart/form-data' with two fields" in {
marshal(MultipartFormData(ListMap("surname" -> BodyPart("Mike"), "age" -> BodyPart(marshal(<int>42</int>).get)))) ===
Right {
HttpEntity(
contentType = ContentType(`multipart/form-data` withBoundary randomBoundary),
string = s"""|--${randomBoundary}
|Content-Disposition: form-data; name=surname
|Content-Type: text/plain
|
|Mike
|--${randomBoundary}
|Content-Disposition: form-data; name=age
|Content-Type: text/xml
|
|<int>42</int>
|--${randomBoundary}--""".stripMargin.replace(EOL, "\r\n"))
}
}
"correctly marshal 'multipart/form-data' with two fields having a custom Content-Disposition" in {
marshal(MultipartFormData(Seq(
BodyPart(
HttpEntity(`text/csv`, "name,age\r\n\"John Doe\",20\r\n"),
List(`Content-Disposition`("form-data", Map("name" -> "attachment[0]", "filename" -> "attachment.csv")))),
BodyPart(
HttpEntity("name,age\r\n\"John Doe\",20\r\n".getBytes),
List(
`Content-Disposition`("form-data", Map("name" -> "attachment[1]", "filename" -> "attachment.csv")),
RawHeader("Content-Transfer-Encoding", "binary")))))) ===
Right {
HttpEntity(
contentType = ContentType(`multipart/form-data` withBoundary randomBoundary),
string = s"""|--${randomBoundary}
|Content-Disposition: form-data; name="attachment[0]"; filename=attachment.csv
|Content-Type: text/csv
|
|name,age
|"John Doe",20
|
|--${randomBoundary}
|Content-Disposition: form-data; name="attachment[1]"; filename=attachment.csv
|Content-Transfer-Encoding: binary
|Content-Type: application/octet-stream
|
|name,age
|"John Doe",20
|
|--${randomBoundary}--""".stripMargin.replace(EOL, "\r\n"))
}
}
}
}