# -*- coding: utf-8 -*-

from base import *
import requests
import socket

ASSERT_RESPONSE = b"Hello world!"
RESPONSE = [b"Hello ", b"world!"]

DEFAULT_HOST = "localhost"
DEFAULT_PORT = 8000
DEFAULT_METHOD = "GET"
DEFAULT_PATH = "/PATH?ket=value"
DEFAULT_VERSION = "HTTP/1.0"

DEFAULT_ADDR = (DEFAULT_HOST, DEFAULT_PORT)

DEFAULT_HEADER = [
            ("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; ja; rv:1.9.2.7) Gecko/20100715 Ubuntu/10.04 (lucid) Firefox/3.6.7"),
            ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
            ("Accept-Language", "ja,en-us;q=0.7,en;q=0.3"),
            ("Accept-Encoding", "gzip,deflate"),
            ("Accept-Charset", "Shift_JIS,utf-8;q=0.7,*;q=0.7"),
            ("Keep-Alive","115"),
            ("Connection", "keep-alive"),
            ("Cache-Control", "max-age=0"),
        ]

def to_bytes(s):
    if isinstance(s, bytes):
        return s
    else:
        return s.encode('iso-8859-1')

def send_data(addr=DEFAULT_ADDR, method=DEFAULT_METHOD, path=DEFAULT_PATH,
        version=DEFAULT_VERSION, headers=DEFAULT_HEADER, post_data=None):

    try:
        sock = socket.create_connection(addr)
        sock.send(to_bytes("%s %s %s\r\n" % (method, path, version)))
        sock.send(to_bytes("Host: %s\r\n" % addr[0]))
        for h in  headers:
            sock.send(to_bytes("%s: %s\r\n" % h))
        sock.send(b"\r\n")
        if post_data:
            sock.send(to_bytes(post_data))
            sock.send(b"\r\n")

        data = sock.recv(1024 * 2)
        return data
    except:
        import traceback
        print(traceback.format_exc())
        raise

class App(BaseApp):

    environ = None

    def __call__(self, environ, start_response):
        status = '200 OK'
        response_headers = [('Content-type','text/plain')]
        start_response(status, response_headers)
        self.environ = environ.copy()
        print(environ)
        return RESPONSE



def test_long_url1():

    def client():
        query = "A" * 8191
        return requests.get("http://localhost:8000/" + query)
    
    env, res = run_client(client, App)
    assert(res.status_code == 200)

def test_long_url2():

    def client():
        query = "A" * 8192
        return requests.get("http://localhost:8000/" + query)
    
    env, res = run_client(client, App)
    assert(res.status_code == 400)

def test_bad_method1():

    def client():
        send_data(method=b"")

    env, res = run_client(client, App)
    assert(res == None)

def test_bad_method2():

    def client():
        send_data(method=b"GET" * 100)

    env, res = run_client(client, App)
    assert(res == None)



@@ -860,16 +860,55 @@
      %5D%0A%0A
+ERR_400 = b%22HTTP/1.0 400 Bad Request%22%0A%0A
 def to_b
@@ -1997,17 +1997,16 @@
 PONSE%0A%0A%0A
-%0A
 def test
@@ -2460,32 +2460,39 @@
 ient():%0A        
+return 
 send_data(method
@@ -2551,24 +2551,44 @@
 sert(res
- == None
+.split(%22%5Cr%5Cn%22)%5B0%5D == ERR_400
 )%0A%0Adef t
@@ -2633,16 +2633,23 @@
         
+return 
 send_dat
@@ -2729,16 +2729,198 @@
 (res
- == None)%0A
+.split(%22%5Cr%5Cn%22)%5B0%5D == ERR_400)%0A%0A%0Adef test_bad_path():%0A%0A    def client():%0A        return send_data(path=b%22..%22)%0A%0A    env, res = run_client(client, App)%0A    assert(res.split(%22%5Cr%5Cn%22)%5B0%5D == ERR_400)
 %0A%0A

Add test