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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
mod constants;
mod msgpack;
mod msgpack_config;
mod msgpack_error;
mod msgpack_message;
mod msgpack_response_builder;

pub(crate) use constants::DEFAULT_PAYLOAD_LIMIT;
pub use msgpack::MsgPack;
pub use msgpack_config::MsgPackConfig;
pub use msgpack_error::MsgPackError;
pub use msgpack_message::MsgPackMessage;
pub use msgpack_response_builder::MsgPackResponseBuilder;

#[cfg(test)]
mod tests {
	use super::*;
	use actix_web::body::MessageBody;
	use actix_web::http::{header, StatusCode};
	use actix_web::test::TestRequest;
	use actix_web::web::Bytes;
	use actix_web::{HttpRequest, HttpResponse, Responder};
	use mime::{APPLICATION_JSON, APPLICATION_MSGPACK};
	use serde::{Deserialize, Serialize};

	impl PartialEq for MsgPackError {
		fn eq(&self, other: &MsgPackError) -> bool {
			match *self {
				MsgPackError::Overflow => {
					matches!(*other, MsgPackError::Overflow)
				},
				MsgPackError::ContentType => {
					matches!(*other, MsgPackError::ContentType)
				},
				_ => false,
			}
		}
	}

	#[derive(Debug, Serialize, Deserialize, PartialEq)]
	pub struct Data {
		payload: bool,
	}

	#[actix_web::test]
	async fn check_content_type() {
		// Pass empty Content-Type
		let (req, mut payload) = TestRequest::default().to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.err().unwrap(), MsgPackError::ContentType);

		// Pass non-msgpack Content-Type
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_JSON))
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.err().unwrap(), MsgPackError::ContentType);

		// Pass correct Content-Type
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_ne!(msgpack.err().unwrap(), MsgPackError::ContentType);
	}

	#[actix_web::test]
	async fn check_default_limit() {
		// Pass min limit
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 0))
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).await;

		assert_ne!(msgpack.err().unwrap(), MsgPackError::Overflow);

		// Pass max limit
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, DEFAULT_PAYLOAD_LIMIT))
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).await;

		assert_ne!(msgpack.err().unwrap(), MsgPackError::Overflow);

		// Pass more than default limit
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, DEFAULT_PAYLOAD_LIMIT + 1))
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).await;

		assert_eq!(msgpack.err().unwrap(), MsgPackError::Overflow);
	}

	#[actix_web::test]
	async fn check_custom_limit() {
		const LIMIT: usize = 10;

		// Pass max limit
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, LIMIT))
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).limit(LIMIT).await;

		assert_ne!(msgpack.err().unwrap(), MsgPackError::Overflow);

		// Pass more than limit
		let (req, mut payload) = TestRequest::default()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, LIMIT + 1))
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).limit(LIMIT).await;

		assert_eq!(msgpack.err().unwrap(), MsgPackError::Overflow);
	}

	#[actix_web::test]
	async fn check_body() {
		// Pass empty body
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.to_http_parts();

		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).await;

		assert!(matches!(msgpack.err().unwrap(), MsgPackError::Payload(..)));

		// Pass invalid body
		let data = Bytes::from_static(&[0x81]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 1))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<()>::new(&req, &mut payload).await;

		assert!(matches!(msgpack.err().unwrap(), MsgPackError::Deserialize(..)));

		// Pass correct body
		let data =
			Bytes::from_static(&[0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 10))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.ok().unwrap(), Data { payload: true })
	}

	#[actix_web::test]
	async fn check_body_limit() {
		// Pass body length == Content-Length value header
		let data =
			Bytes::from_static(&[0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 10))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.ok().unwrap(), Data { payload: true });

		// Pass body length < Content-Length value header
		let data =
			Bytes::from_static(&[0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 11))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.ok().unwrap(), Data { payload: true });

		// Pass body length > Content-Length value header
		let data =
			Bytes::from_static(&[0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.insert_header((header::CONTENT_LENGTH, 1))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.ok().unwrap(), Data { payload: true });

		// Pass body and don't pass Content-Length header
		let data =
			Bytes::from_static(&[0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]);
		let (req, mut payload) = TestRequest::post()
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.set_payload(data)
			.to_http_parts();
		let msgpack = MsgPackMessage::<Data>::new(&req, &mut payload).await;

		assert_eq!(msgpack.ok().unwrap(), Data { payload: true });
	}

	#[actix_web::test]
	async fn check_responses() {
		// Response with msgpack responder
		async fn service(_: HttpRequest) -> HttpResponse {
			let payload = Data { payload: true };
			HttpResponse::Ok().msgpack(payload)
		}

		let request = TestRequest::post()
			.uri("/")
			.insert_header((header::CONTENT_TYPE, APPLICATION_MSGPACK))
			.to_http_request();
		let response = service(request).await;

		assert_eq!(response.status(), StatusCode::OK);
		assert_eq!(
			response.into_body().try_into_bytes().unwrap(),
			vec![0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]
		);
	}

	#[actix_web::test]
	async fn check_responder() {
		// Pass correct payload
		let request = TestRequest::default().to_http_request();
		let payload = MsgPack(Bytes::from_static(&[
			0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3,
		]));
		let response = payload.clone().respond_to(&request);

		assert_eq!(response.status(), StatusCode::OK);
		assert_eq!(
			response.into_body().try_into_bytes().unwrap(),
			vec![0x81, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0xc3]
		);
	}
}