var Buffer = require('buffer').Buffer
var fs = require('fs')
var keys = require('object-keys')
var test = require('tape')
var UAParser = require('ua-parser-js')

var http = require('../..')

function indexOf (arr, item) {
	var len = arr.length
	for (var i = 0; i < len; i++) {
		if (arr[i] === item)
			return i
	}
	return -1
}

test('headers', function (t) {
	http.get({
		path: '/testHeaders?Response-Header=bar&Response-Header-2=BAR2',
		headers: {
			'Test-Request-Header': 'foo',
			'Test-Request-Header-2': 'FOO2'
		}
	}, function (res) {
		var rawHeaders = []
		for (var i = 0; i < res.rawHeaders.length; i += 2) {
			var lowerKey = res.rawHeaders[i].toLowerCase()
			if (lowerKey.indexOf('test-') === 0)
				rawHeaders.push(lowerKey, res.rawHeaders[i + 1])
		}
		var header1Pos = indexOf(rawHeaders, 'test-response-header')
		t.ok(header1Pos >= 0, 'raw response header 1 present')
		t.equal(rawHeaders[header1Pos + 1], 'bar', 'raw response header value 1')
		var header2Pos = indexOf(rawHeaders, 'test-response-header-2')
		t.ok(header2Pos >= 0, 'raw response header 2 present')
		t.equal(rawHeaders[header2Pos + 1], 'BAR2', 'raw response header value 2')
		t.equal(rawHeaders.length, 4, 'correct number of raw headers')

		t.equal(res.headers['test-response-header'], 'bar', 'response header 1')
		t.equal(res.headers['test-response-header-2'], 'BAR2', 'response header 2')

		var buffers = []

		res.on('end', function () {
			var body = JSON.parse(Buffer.concat(buffers).toString())
			t.equal(body['test-request-header'], 'foo', 'request header 1')
			t.equal(body['test-request-header-2'], 'FOO2', 'request header 2')
			t.equal(keys(body).length, 2, 'correct number of request headers')
			t.end()
		})

		res.on('data', function (data) {
			buffers.push(data)
		})
	})
})

test('content-type response header', function (t) {
	http.get('/testHeaders', function (res) {
		t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
		t.end()
	})
})

var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
var browserName = browser.name
var browserVersion = browser.major
// The content-type header is broken when 'prefer-streaming' or 'allow-wrong-content-type'
// is passed in browsers that rely on xhr.overrideMimeType(), namely older chrome and newer safari
var wrongMimeType = ((browserName === 'Chrome' && browserVersion <= 42) ||
	((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion >= 6))

test('content-type response header with forced streaming', function (t) {
	http.get({
		path: '/testHeaders',
		mode: 'prefer-streaming'
	}, function (res) {
		if (wrongMimeType)
			t.equal(res.headers['content-type'], 'text/plain; charset=x-user-defined', 'content-type overridden')
		else
			t.equal(res.headers['content-type'], 'application/json', 'content-type preserved')
		t.end()
	})
})
@@ -58,42 +58,8 @@
 s')%0A
-var keys = require('object-keys')%0A
 var 
@@ -81,16 +81,16 @@
 'tape')%0A
+
 var UAPa
@@ -154,146 +154,8 @@
 ')%0A%0A
-function indexOf (arr, item) %7B%0A%09var len = arr.length%0A%09for (var i = 0; i %3C len; i++) %7B%0A%09%09if (arr%5Bi%5D === item)%0A%09%09%09return i%0A%09%7D%0A%09return -1%0A%7D%0A%0A
 test
@@ -605,32 +605,24 @@
 eader1Pos = 
-indexOf(
 rawHeaders, 
@@ -611,34 +611,41 @@
 Pos = rawHeaders
-, 
+.indexOf(
 'test-response-h
@@ -804,24 +804,16 @@
 r2Pos = 
-indexOf(
 rawHeade
@@ -814,18 +814,25 @@
 wHeaders
-, 
+.indexOf(
 'test-re
@@ -1450,16 +1450,16 @@
 der 2')%0A
-
 %09%09%09t.equ
@@ -1461,16 +1461,23 @@
 t.equal(
+Object.
 keys(bod

Fix tests