@@ -5,55 +5,70 @@ var mapper = require('./mapper');
55var qs = require ( 'querystring' ) ;
66var targets = require ( './targets' ) ;
77var url = require ( 'url' ) ;
8+ var validate = require ( 'har-validator' ) ;
89var util = require ( 'util' ) ;
910
1011// constructor
1112var HTTPSnippet = function ( req , lang ) {
1213 this . source = util . _extend ( { } , req ) ;
1314
14- // construct query string object
15- this . source . queryObj = { } ;
16- this . source . headersObj = { } ;
15+ // add optional properties to make validation successful
16+ this . source . httpVersion = this . source . httpVersion || 'HTTP/1.1' ;
17+ this . source . queryString = this . source . queryString || [ ] ;
18+ this . source . headers = this . source . headers || [ ] ;
19+ this . source . cookies = this . source . cookies || [ ] ;
20+ this . source . postData = this . source . postData || { } ;
21+ this . source . postData . mimeType = this . source . postData . mimeType || 'application/x-www-form-urlencoded' ;
22+
23+ this . source . bodySize = 0 ;
24+ this . source . headersSize = 0 ;
25+ this . source . postData . size = 0 ;
26+
27+ validate . request ( this . source , function ( err , valid ) {
28+ if ( ! valid ) {
29+ throw err ;
30+ }
1731
18- if ( this . source . url === undefined ) {
19- throw new Error ( 'a request url is required' ) ;
20- }
32+ // construct query string object
33+ this . source . queryObj = { } ;
34+ this . source . headersObj = { } ;
2135
22- // construct query objects
23- if ( this . source . queryString && this . source . queryString . length ) {
24- debug ( 'queryString found, constructing queryString pair map' ) ;
36+ // construct query objects
37+ if ( this . source . queryString && this . source . queryString . length ) {
38+ debug ( 'queryString found, constructing queryString pair map' ) ;
2539
26- this . source . queryString . map ( mapper ( this . source . queryObj ) ) ;
27- }
40+ this . source . queryString . map ( mapper ( this . source . queryObj ) ) ;
41+ }
2842
29- // construct headers objects
30- if ( this . source . headers && this . source . headers . length ) {
31- debug ( 'headers found, constructing header pair map' ) ;
43+ // construct headers objects
44+ if ( this . source . headers && this . source . headers . length ) {
45+ debug ( 'headers found, constructing header pair map' ) ;
3246
33- this . source . headers . map ( mapper ( this . source . headersObj ) ) ;
34- }
47+ this . source . headers . map ( mapper ( this . source . headersObj ) ) ;
48+ }
3549
36- // deconstruct the uri
37- this . source . uriObj = url . parse ( this . source . url , true , true ) ;
50+ // deconstruct the uri
51+ this . source . uriObj = url . parse ( this . source . url , true , true ) ;
3852
39- // merge all possible queryString values
40- this . source . queryString = util . _extend ( this . source . uriObj . query , this . source . queryObj ) ;
53+ // merge all possible queryString values
54+ this . source . queryString = util . _extend ( this . source . uriObj . query , this . source . queryObj ) ;
4155
42- // reset uriObj values for a clean url
43- this . source . uriObj . query = null ;
44- this . source . uriObj . search = null ;
45- this . source . uriObj . path = this . source . uriObj . pathname ;
56+ // reset uriObj values for a clean url
57+ this . source . uriObj . query = null ;
58+ this . source . uriObj . search = null ;
59+ this . source . uriObj . path = this . source . uriObj . pathname ;
4660
47- // keep the base url clean of queryString
48- this . source . url = url . format ( this . source . uriObj ) ;
61+ // keep the base url clean of queryString
62+ this . source . url = url . format ( this . source . uriObj ) ;
4963
50- // update the uri object
51- this . source . uriObj . query = this . source . queryString ;
52- this . source . uriObj . search = qs . stringify ( this . source . queryString ) ;
53- this . source . uriObj . path = this . source . uriObj . pathname + '?' + this . source . uriObj . search ;
64+ // update the uri object
65+ this . source . uriObj . query = this . source . queryString ;
66+ this . source . uriObj . search = qs . stringify ( this . source . queryString ) ;
67+ this . source . uriObj . path = this . source . uriObj . pathname + '?' + this . source . uriObj . search ;
5468
55- // construct a full url
56- this . source . fullUrl = url . format ( this . source . uriObj ) ;
69+ // construct a full url
70+ this . source . fullUrl = url . format ( this . source . uriObj ) ;
71+ } . bind ( this ) ) ;
5772} ;
5873
5974HTTPSnippet . prototype . getSource = function ( ) {
0 commit comments