4 var qs = require('querystring')
5 var validate = require('har-validator')
6 var extend = require('extend')
8 function Har (request) {
12 Har.prototype.reducer = function (obj, pair) {
14 if (obj[pair.name] === undefined) {
15 obj[pair.name] = pair.value
19 // existing? convert to array
30 Har.prototype.prep = function (data) {
31 // construct utility properties
34 data.postData.jsonObj = false
35 data.postData.paramsObj = false
37 // construct query objects
38 if (data.queryString && data.queryString.length) {
39 data.queryObj = data.queryString.reduce(this.reducer, {})
42 // construct headers objects
43 if (data.headers && data.headers.length) {
44 // loweCase header keys
45 data.headersObj = data.headers.reduceRight(function (headers, header) {
46 headers[header.name] = header.value
51 // construct Cookie header
52 if (data.cookies && data.cookies.length) {
53 var cookies = data.cookies.map(function (cookie) {
54 return cookie.name + '=' + cookie.value
58 data.headersObj.cookie = cookies.join('; ')
64 return arr.some(function (type) {
65 return data.postData.mimeType.indexOf(type) === 0
72 'multipart/form-data',
73 'multipart/alternative'])) {
76 data.postData.mimeType = 'multipart/form-data'
80 'application/x-www-form-urlencoded'])) {
82 if (!data.postData.params) {
83 data.postData.text = ''
85 data.postData.paramsObj = data.postData.params.reduce(this.reducer, {})
88 data.postData.text = qs.stringify(data.postData.paramsObj)
96 'application/x-json'])) {
98 data.postData.mimeType = 'application/json'
100 if (data.postData.text) {
102 data.postData.jsonObj = JSON.parse(data.postData.text)
104 this.request.debug(e)
106 // force back to text/plain
107 data.postData.mimeType = 'text/plain'
115 Har.prototype.options = function (options) {
116 // skip if no har property defined
122 extend(har, options.har)
124 // only process the first entry
125 if (har.log && har.log.entries) {
126 har = har.log.entries[0]
129 // add optional properties to make validation successful
130 har.url = har.url || options.url || options.uri || options.baseUrl || '/'
131 har.httpVersion = har.httpVersion || 'HTTP/1.1'
132 har.queryString = har.queryString || []
133 har.headers = har.headers || []
134 har.cookies = har.cookies || []
135 har.postData = har.postData || {}
136 har.postData.mimeType = har.postData.mimeType || 'application/octet-stream'
140 har.postData.size = 0
142 if (!validate.request(har)) {
146 // clean up and get some utility properties
147 var req = this.prep(har)
149 // construct new options
151 options.url = req.url
155 options.method = req.method
158 if (Object.keys(req.queryObj).length) {
159 options.qs = req.queryObj
162 if (Object.keys(req.headersObj).length) {
163 options.headers = req.headersObj
166 function test (type) {
167 return req.postData.mimeType.indexOf(type) === 0
169 if (test('application/x-www-form-urlencoded')) {
170 options.form = req.postData.paramsObj
172 else if (test('application/json')) {
173 if (req.postData.jsonObj) {
174 options.body = req.postData.jsonObj
178 else if (test('multipart/form-data')) {
179 options.formData = {}
181 req.postData.params.forEach(function (param) {
184 if (!param.fileName && !param.fileName && !param.contentType) {
185 options.formData[param.name] = param.value
189 // attempt to read from disk!
190 if (param.fileName && !param.value) {
191 attachment.value = fs.createReadStream(param.fileName)
192 } else if (param.value) {
193 attachment.value = param.value
196 if (param.fileName) {
197 attachment.options = {
198 filename: param.fileName,
199 contentType: param.contentType ? param.contentType : null
203 options.formData[param.name] = attachment
207 if (req.postData.text) {
208 options.body = req.postData.text