This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
forked from znewman01/rust-har
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
1850 lines (1690 loc) · 61 KB
/
lib.rs
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Implements struct hierarchy and serializer for the [HAR 1.2 spec][1].
///
/// [1]: http://www.softwareishard.com/blog/har-12-spec/
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use serde::de::{Deserialize, Deserializer};
const HAR_VERSION: &'static str = "1.2";
const HAR_CREATOR_NAME: &'static str = "Rust-HAR";
const HAR_CREATOR_VERSION: &'static str = "0.0.4";
/// This object represents the root of the exported data.
///
/// This object MUST be present and its name MUST be "log".
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Log {
/// Version number of the format.
version: String,
/// Name and version info of the log creator application.
creator: Creator,
/// Name and version info of used browser.
browser: Option<Browser>,
/// List of all exported (tracked) pages.
/// Leave out this field if the application does not support grouping by pages.
pages: Option<Vec<Page>>,
/// List of all exported (tracked) requests.
entries: Vec<Entry>,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Log {
pub fn new(browser: Option<Browser>, comment: Option<String>) -> Log {
Log {
version: HAR_VERSION.to_string(),
creator: Creator::new(
HAR_CREATOR_NAME.to_string(),
HAR_CREATOR_VERSION.to_string(),
None
),
browser: browser,
pages: None,
entries: Vec::new(),
comment: comment
}
}
pub fn add_page(&mut self, page: Page) {
match self.pages {
Some(ref mut pages) => pages.push(page),
None => self.pages = Some(vec![page])
}
}
pub fn add_entry(&mut self, entry: Entry) {
self.entries.push(entry);
}
}
/// This object contains information about the log creator application.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Creator {
name: String,
version: String,
comment: Option<String>
}
impl Creator {
pub fn new(name: String, version: String, comment: Option<String>) -> Creator{
Creator {
name: name,
version: version,
comment: comment
}
}
}
/// This object contains information about the browser that created the log.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Browser {
name: String,
version: String,
comment: Option<String>
}
impl Browser {
pub fn new(name: String, version: String, comment: Option<String>) -> Browser{
Browser {
name: name,
version: version,
comment: comment
}
}
}
/// This object represents list of exported pages.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Page {
/// Date and time stamp for the beginning of the page load
/// (ISO 8601 YYYY-MM-DDThh:mm:ss.sTZD, e.g. 2009-07-24T19:20:30.45+01:00).
started_date_time: String,
/// Unique identifier of a page within the . Entries use it to refer the parent page.
id: String,
/// Page title.
title: String,
/// Detailed timing info about page load.
page_timings: PageTimings,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Page {
pub fn new(started_date_time: String,
id: String,
title: String,
page_timings: PageTimings,
comment: Option<String>) -> Page {
Page {
started_date_time: started_date_time,
id: id,
title: title,
page_timings: page_timings,
comment: comment
}
}
}
/// This object describes timings for various events (states) fired during the page load.
///
/// All times are specified in milliseconds.
/// If a time info is not available appropriate field is set to -1.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PageTimings {
/// Content of the page loaded.
/// Number of milliseconds since page load started (page.startedDateTime).
/// Use -1 if the timing does not apply to the current request.
on_content_load: OptionalTiming,
/// Page is loaded (onLoad event fired).
/// Number of milliseconds since page load started (page.startedDateTime).
/// Use -1 if the timing does not apply to the current request.
on_load: OptionalTiming,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl PageTimings {
pub fn new(
on_content_load: OptionalTiming,
on_load: OptionalTiming,
comment: Option<String>
) -> PageTimings {
PageTimings {
on_content_load: on_content_load,
on_load: on_load,
comment: comment,
}
}
}
/// This object represents an array with all exported HTTP requests. Sorting entries by
/// startedDateTime (starting from the oldest) is preferred way how to export data since it can
/// make importing faster. However the reader application should always make sure the array is
/// sorted (if required for the import).
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Entry {
/// Reference to the parent page (unique).
/// Leave out this field if the application does not support grouping by pages.
pageref: Option<String>,
/// Date and time stamp of the request start (ISO 8601 YYYY-MM-DDThh:mm:ss.sTZD).
started_date_time: String,
/// Total elapsed time of the request in milliseconds.
/// This is the sum of all timings available in the timings object.
// time [number]
/// Detailed info about the request.
request: Request,
/// Detailed info about the response.
response: Response,
/// Info about cache usage.
cache: Cache,
/// Detailed timing info about request/response round trip.
timings: Timing,
/// IP address of the server that was connected (result of DNS resolution).
server_ip_address: Option<String>,
/// Unique ID of the parent TCP/IP connection, can be the client port number.
///
/// Note that a port number doesn't have to be unique identifier in cases where the port is
/// shared for more connections. If the port isn't available for the application, any other
/// unique connection ID can be used instead (e.g. connection index). Leave out this field if
/// the application doesn't support this info.
connection: Option<String>,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Entry {
pub fn new(
pageref: Option<String>,
started_date_time: String,
request: Request,
response: Response,
cache: Cache,
timings: Timing,
server_ip_address: Option<String>,
connection: Option<String>,
comment: Option<String>
) -> Entry {
Entry {
pageref: pageref,
started_date_time: started_date_time,
request: request,
response: response,
cache: cache,
timings: timings,
server_ip_address: server_ip_address,
connection: connection,
comment: comment
}
}
}
/// This object contains detailed info about performed request.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Request {
/// Request method (GET, POST, ...).
method: String,
/// Absolute URL of the request (fragments are not included).
url: String,
/// Request HTTP Version.
http_version: String,
/// List of cookie objects.
cookies: Vec<Cookie>,
/// List of header objects.
headers: Vec<Header>,
/// List of query parameter objects.
query_string: Vec<QueryStringPair>,
/// Posted data info.
post_data: Option<PostData>,
/// Total number of bytes from the start of the HTTP request message until (and including)
/// the double CRLF before the body.
/// Set to -1 if the info is not available.
headers_size: Option<i32>,
/// Size of the request body (POST data payload) in bytes.
/// Set to -1 if the info is not available.
body_size: Option<i32>,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Request {
pub fn new(
method: String,
url: String,
http_version: String,
cookies: Vec<Cookie>,
headers: Vec<Header>,
query_string: Vec<QueryStringPair>,
post_data: Option<PostData>,
headers_size: Option<i32>,
body_size: Option<i32>,
comment: Option<String>
) -> Request {
Request {
method: method,
url: url,
http_version: http_version,
cookies: cookies,
headers: headers,
query_string: query_string,
post_data: post_data,
headers_size: headers_size,
body_size: body_size,
comment: comment
}
}
}
/// This object contains detailed info about the response.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Response {
/// Response status.
status: i32,
/// Response status description.
status_text: String,
/// Response HTTP Version.
http_version: String,
/// List of cookie objects.
cookies: Vec<Cookie>,
/// List of header objects.
headers: Vec<Header>,
/// Details about the response body.
content: Content,
/// Redirection target URL from the Location response header.
#[serde(rename = "redirectURL")]
redirect_url: String,
/// Total number of bytes from the start of the HTTP response message until (and including) the
/// double CRLF before the body.
/// Set to -1 if the info is not available.
/// The size of received response-headers is computed only from headers that are really
/// received from the server. Additional headers appended by the browser are not included in
/// this number, but they appear in the list of header objects.
headers_size: Option<i32>,
/// Size of the received response body in bytes.
/// Set to zero in case of responses coming from the cache (304).
/// Set to -1 if the info is not available.
body_size: Option<i32>,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Response {
pub fn new(
status: i32,
status_text: String,
http_version: String,
cookies: Vec<Cookie>,
headers: Vec<Header>,
content: Content,
redirect_url: String,
headers_size: Option<i32>,
body_size: Option<i32>,
comment: Option<String>
) -> Response {
Response {
status: status,
status_text: status_text,
http_version: http_version,
cookies: cookies,
headers: headers,
content: content,
redirect_url: redirect_url,
headers_size: headers_size,
body_size: body_size,
comment: comment
}
}
}
/// This object contains list of all cookies (used in <request> and <response> objects).
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Cookie {
/// The name of the cookie.
name: String,
/// The cookie value.
value: String,
/// The path pertaining to the cookie.
path: Option<String>,
/// The host of the cookie.
domain: Option<String>,
/// Cookie expiration time. (ISO 8601).
expires: Option<String>,
/// Set to true if the cookie is HTTP only, false otherwise.
http_only: Option<bool>,
/// True if the cookie was transmitted over ssl, false otherwise.
secure: Option<bool>,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl Cookie {
pub fn new(
name: String,
value: String,
path: Option<String>,
domain: Option<String>,
expires: Option<String>,
http_only: Option<bool>,
secure: Option<bool>,
comment: Option<String>
) -> Cookie {
Cookie {
name: name,
value: value,
path: path,
domain: domain,
expires: expires,
http_only: http_only,
secure: secure,
comment: comment
}
}
}
/// This object contains list of all headers (used in <request> and <response> objects).
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Header {
name: String,
value: String,
comment: Option<String>
}
impl Header {
pub fn new(
name: String,
value: String,
comment: Option<String>
) -> Header {
Header {
name: name,
value: value,
comment: comment
}
}
}
/// This object contains list of all parameters & values parsed from a query string, if any
/// (embedded in <request> object).
/// HAR format expects NVP (name-value pairs) formatting of the query string.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryStringPair {
name: String,
value: String,
comment: Option<String>
}
impl QueryStringPair {
pub fn new(
name: String,
value: String,
comment: Option<String>
) -> QueryStringPair {
QueryStringPair {
name: name,
value: value,
comment: comment
}
}
}
/// This object describes posted data, if any (embedded in <request> object).
/// Note that text and params fields are mutually exclusive.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PostData {
/// Mime type of posted data.
mime_type: String,
/// List of posted parameters (in case of URL encoded parameters).
params: Vec<Param>,
/// Plain text posted data
text: String,
/// A comment provided by the user or the application.
comment: Option<String>
}
impl PostData {
pub fn new(
mime_type: String,
params: Vec<Param>,
text: String,
comment: Option<String>
) -> PostData {
PostData {
mime_type: mime_type,
params: params,
text: text,
comment: comment
}
}
}
/// List of posted parameters, if any (embedded in <postData> object).
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Param {
/// name of a posted parameter.
name: String,
/// value of a posted parameter or content of a posted file.
value: Option<String>,
/// name of a posted file.
file_name: Option<String>,
/// content type of a posted file.
content_type: Option<String>,
/// A comment provided by the user or the application.
comment: Option<String>,
}
impl Param {
pub fn new(
name: String,
value: Option<String>,
file_name: Option<String>,
content_type: Option<String>,
comment: Option<String>
) -> Param {
Param {
name: name,
value: value,
file_name: file_name,
content_type: content_type,
comment: comment
}
}
}
/// This object describes details about response content (embedded in <response> object).
///
/// Before setting the text field, the HTTP response is decoded (decompressed & unchunked), than
/// trans-coded from its original character set into UTF-8. Additionally, it can be encoded using
/// e.g. base64. Ideally, the application should be able to unencode a base64 blob and get a
/// byte-for-byte identical resource to what the browser operated on.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Content {
/// Length of the returned content in bytes.
/// Should be equal to response.bodySize if there is no compression and bigger when the content
/// has been compressed.
size: i32,
/// Number of bytes saved. Leave out this field if the information is not available.
compression: Option<i32>,
/// MIME type of the response text (value of the Content-Type response header).
/// The charset attribute of the MIME type is included (if available).
mime_type: String,
/// Response body sent from the server or loaded from the browser cache.
/// This field is populated with textual content only.
/// The text field is either HTTP decoded text or a encoded (e.g. "base64") representation of
/// the response body.
/// Leave out this field if the information is not available.
text: Option<String>,
/// Encoding used for response text field e.g "base64".
/// Leave out this field if the text field is HTTP decoded (decompressed & unchunked),
/// than trans-coded from its original character set into UTF-8.
encoding: Option<String>,
/// A comment provided by the user or the application.
comment: Option<String>,
}
impl Content {
pub fn new(
size: i32,
compression: Option<i32>,
mime_type: String,
text: Option<String>,
encoding: Option<String>,
comment: Option<String>
) -> Content {
Content {
size: size,
compression: compression,
mime_type: mime_type,
text: text,
encoding: encoding,
comment: comment
}
}
}
/// This objects contains info about a request coming from browser cache.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Cache {
/// State of a cache entry before the request.
/// Leave out this field if the information is not available.
#[serde(default = "CacheState::unknown")]
before_request: CacheState,
/// State of a cache entry after the request.
/// Leave out this field if the information is not available.
#[serde(default = "CacheState::unknown")]
after_request: CacheState,
comment: Option<String>
}
impl Cache {
pub fn new(
before_request: CacheState,
after_request: CacheState,
comment: Option<String>
) -> Cache {
Cache {
before_request: before_request,
after_request: after_request,
comment: comment
}
}
}
/// The state of a cache entry.
///
/// Can be Absent, Present, or Unknown. When serialized, these result in (respectively) `null`, a
/// CacheEntry value, or no object.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum CacheState {
Absent,
Present(CacheEntry),
Unknown
}
impl CacheState {
fn unknown() -> Self { CacheState::Unknown }
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CacheEntry {
/// Expiration time of the cache entry.
expires: Option<String>,
/// The last time the cache entry was opened.
last_access: String,
/// Etag
e_tag: String,
/// The number of times the cache entry has been opened.
hit_count: i32,
/// (new in 1.2) A comment provided by the user or the application.
comment: Option<String>,
}
impl CacheEntry {
pub fn new(
expires: Option<String>,
last_access: String,
e_tag: String,
hit_count: i32,
comment: Option<String>
) -> CacheEntry {
CacheEntry {
expires: expires,
last_access: last_access,
e_tag: e_tag,
hit_count: hit_count,
comment: comment
}
}
}
/// A timing value which may be absent or present
///
/// Defaults to -1 in the absent case.
#[derive(Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum OptionalTiming {
TimedContent(u32),
NotApplicable
}
impl Deserialize for OptionalTiming {
fn deserialize<D>(deserializer: D) -> Result<OptionalTiming, D::Error>
where D: Deserializer
{
let deser_result: serde_json::Value = try!(serde::Deserialize::deserialize(deserializer));
match deser_result {
serde_json::Value::Number(ref n) if n.as_i64().is_some() && n.as_i64().unwrap() >= 0 =>
Ok(OptionalTiming::TimedContent(n.as_u64().unwrap() as u32)),
serde_json::Value::Number(ref n) if n.as_i64().is_some() && n.as_i64().unwrap() == -1 =>
Ok(OptionalTiming::NotApplicable),
_ => Err(serde::de::Error::custom("Unexpected value")),
}
}
}
/// This object describes various phases within request-response round trip. All times are
/// specified in milliseconds.
///
/// The send, wait and receive timings are not optional and must have non-negative values.
///
/// An exporting tool can omit the blocked, dns, connect and ssl, timings on every request if it is
/// unable to provide them. Tools that can provide these timings can set their values to -1 if they
/// don’t apply. For example, connect would be -1 for requests which re-use an existing connection.
///
/// The time value for the request must be equal to the sum of the timings supplied in this section
/// (excluding any -1 values).
///
/// Following must be true in case there are no -1 values (entry is an object in log.entries) :
/// entry.time == entry.timings.blocked + entry.timings.dns +
/// entry.timings.connect + entry.timings.send + entry.timings.wait +
/// entry.timings.receive;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Timing {
/// Time spent in a queue waiting for a network connection.
/// Use -1 if the timing does not apply to the current request.
blocked: OptionalTiming,
/// DNS resolution time. The time required to resolve a host name.
/// Use -1 if the timing does not apply to the current request.
dns: OptionalTiming,
/// Time required to create TCP connection.
/// Use -1 if the timing does not apply to the current request.
connect: OptionalTiming,
/// Time required to send HTTP request to the server.
send: u32,
/// Waiting for a response from the server.
wait: u32,
/// Time required to read entire response from the server (or cache).
receive: u32,
/// Time required for SSL/TLS negotiation.
/// If this field is defined then the time is also included in the connect field (to ensure
/// backward compatibility with HAR 1.1).
/// Use -1 if the timing does not apply to the current request.
ssl: OptionalTiming,
/// (new in 1.2) - A comment provided by the user or the application.
comment: Option<String>
}
impl Timing {
pub fn new(
blocked: OptionalTiming,
dns: OptionalTiming,
connect: OptionalTiming,
send: u32,
wait: u32,
receive: u32,
ssl: OptionalTiming,
comment: Option<String>
) -> Timing {
Timing {
blocked: blocked,
dns: dns,
connect: connect,
send: send,
wait: wait,
receive: receive,
ssl: ssl,
comment: comment
}
}
}
#[cfg(test)]
mod test {
use serde_json;
use Browser;
use Cache;
use CacheState::{Absent,Present,Unknown};
use CacheEntry;
use Content;
use Cookie;
use Creator;
use Entry;
use Header;
use Log;
use OptionalTiming::{TimedContent,NotApplicable};
use Page;
use PageTimings;
use Param;
use PostData;
use QueryStringPair;
use Request;
use Response;
use Timing;
#[test]
fn test_log() {
let mut log = Log::new(
Some(Browser::new("Firefox".to_string(), "3.6".to_string(), None)),
Some("Comment".to_string())
);
log.add_page(Page::new(
"2009-04-16T12:07:25.123+01:00".to_string(),
"page_0".to_string(),
"Test Page".to_string(),
PageTimings::new(NotApplicable, NotApplicable, None),
None
));
log.add_entry(Entry::new(
Some("page_0".to_string()),
"2009-04-16T12:07:23.596Z".to_string(),
Request::new(
"GET".to_string(),
"http://www.example.com/path/?param=value".to_string(),
"HTTP/1.1".to_string(),
Vec::new(),
Vec::new(),
Vec::new(),
None,
None,
None,
None
),
Response::new(
200,
"OK".to_string(),
"HTTP/1.1".to_string(),
Vec::new(),
Vec::new(),
Content::new(
100,
None,
"text/html; charset=utf8".to_string(),
None,
None,
None
),
"".to_string(),
None,
None,
None
),
Cache::new(
Absent,
Absent,
None
),
Timing::new(
NotApplicable,
NotApplicable,
NotApplicable,
4,
5,
6,
NotApplicable,
None
),
None,
None,
None
));
let log_json = "{
\"version\": \"1.2\",
\"creator\": {
\"name\": \"Rust-HAR\",
\"version\": \"0.0.4\"
},
\"browser\": {
\"name\": \"Firefox\",
\"version\": \"3.6\"
},
\"pages\": [
{
\"startedDateTime\": \"2009-04-16T12:07:25.123+01:00\",
\"id\": \"page_0\",
\"title\": \"Test Page\",
\"pageTimings\": {
\"onContentLoad\": -1,
\"onLoad\": -1
}
}
],
\"entries\": [
{
\"pageref\": \"page_0\",
\"startedDateTime\": \"2009-04-16T12:07:23.596Z\",
\"request\": {
\"method\": \"GET\",
\"url\": \"http://www.example.com/path/?param=value\",
\"httpVersion\": \"HTTP/1.1\",
\"cookies\": [],
\"headers\": [],
\"queryString\": [],
\"headersSize\": null,
\"bodySize\": null
},
\"response\": {
\"status\": 200,
\"statusText\": \"OK\",
\"httpVersion\": \"HTTP/1.1\",
\"cookies\": [],
\"headers\": [],
\"content\": {
\"size\": 100,
\"mimeType\": \"text/html; charset=utf8\"
},
\"redirectURL\": \"\",
\"headersSize\": null,
\"bodySize\": null
},
\"cache\": {
\"beforeRequest\": null,
\"afterRequest\": null
},
\"time\": 15,
\"timings\": {
\"blocked\": -1,
\"dns\": -1,
\"connect\": -1,
\"send\": 4,
\"wait\": 5,
\"receive\": 6,
\"ssl\": -1
}
}
],
\"comment\": \"Comment\"
}";
let log_from_str: Log = serde_json::from_str(log_json).unwrap();
assert_eq!(log_from_str, log);
}
#[test]
fn test_log_no_optional() {
let log = Log::new(None, None);
let log_json = "{
\"version\": \"1.2\",
\"creator\": {
\"name\": \"Rust-HAR\",
\"version\": \"0.0.4\"
},
\"entries\": []
}";
let log_from_str: Log = serde_json::from_str(log_json).unwrap();
assert_eq!( log_from_str, log );
}
#[test]
fn test_creator() {
let creator = Creator::new(
"Firebug".to_string(),
"1.6".to_string(),
Some("Comment".to_string())
);
let creator_json = "{
\"name\": \"Firebug\",
\"version\": \"1.6\",
\"comment\": \"Comment\"
}";
let creator_from_str: Creator = serde_json::from_str(creator_json).unwrap();
assert_eq!( creator_from_str, creator );
}
#[test]
fn test_creator_no_optional() {