package org.bench4q.recorder;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.httpclient.HttpException;
import org.bench4q.recorder.httpcapture.HttpRequestHeader;
import org.junit.Test;

public class TestHttpRequestHeader {
	private static final String PATHNAME = Test_Bench4QCodeGenerator.dealWithSerie
			+ "Request.txt";
	private HttpRequestHeader requestHeader;

	private HttpRequestHeader getRequestHeader() {
		return requestHeader;
	}

	private void setRequestHeader(HttpRequestHeader requestHeader) {
		this.requestHeader = requestHeader;
	}

	public TestHttpRequestHeader() throws FileNotFoundException, IOException,
			HttpException {
		this.setRequestHeader(new HttpRequestHeader(new FileInputStream(
				new File(PATHNAME))));
	}

	@Test
	public void testHeaders() {
		assertEquals("GET", this.getRequestHeader().method.toUpperCase());
		assertEquals("HTTP://WWW.BAIDU.COM/",
				this.getRequestHeader().referer.toUpperCase());
		assertEquals(
				"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
				this.getRequestHeader().accept.toLowerCase().trim());
		assertEquals("/phoenix.zhtml?c=188488&p=irol-homeprofile",
				this.getRequestHeader().url.toLowerCase().trim());
		assertEquals("HTTP/1.1", this.getRequestHeader().version.toUpperCase()
				.trim());
		assertEquals(
				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36",
				this.getRequestHeader().userAgent.trim());
		assertEquals("bearer", this.getRequestHeader().authorization.trim());
		assertEquals("multipart", this.getRequestHeader().contentType.trim());
		assertEquals(100, this.getRequestHeader().contentLength);
		assertEquals(true, this.getRequestHeader().pragmaNoCache);
		assertEquals("zh-CN,zh;q=0.8", this.getRequestHeader().acceptLanguage);
		// assertEquals("Mon, 22 Mar 2010 14:14:40 GMT; old-content-length=200",
		// this.getRequestHeader().ifModifiedSince.trim());

	}

	@Test
	public void testStripProxyInfoFromRequestHeader() throws Exception {
		HttpRequestHeader header = new HttpRequestHeader(new FileInputStream(
				new File(PATHNAME)));
		String res = "";
		String origUrl = "http://ir.baidu.com/phoenix.zhtml?c=188488&p=irol-homeprofile";

		URL url = new URL(origUrl);
		header.url = url.getFile();
		res = header.toString();
		header.url = origUrl;
		System.out.println(res);
	}

	// @Test
	// public void testToStringWithLib() throws IOException {
	// assertEquals(FileUtils.readFileToString(new File(PATHNAME))
	// .toLowerCase(), this.getRequestHeader().toStringWithLib()
	// .toLowerCase());
	// }
}
