from __future__ import print_function, absolute_import
import pytest
import s
import requests
import json


def test_get_timeout():
    @s.async.coroutine
    def handler(request):
        yield s.async.sleep(1)
        raise s.async.Return({})

    @s.async.coroutine
    def main(url):
        yield s.web.get(url, timeout=.001)

    app = s.web.server([('/', {'GET': handler})])
    with s.web.test(app) as url:
        with pytest.raises(s.web.Timeout):
            s.async.run_sync(lambda: main(url))


def test_get():
    @s.async.coroutine
    def handler(request):
        yield s.async.moment
        raise s.async.Return({'body': 'ok',
                              'code': 200,
                              'headers': {'foo': 'bar'}})

    @s.async.coroutine
    def main(url):
        resp = yield s.web.get(url)
        assert resp['body'] == 'ok'
        assert resp['code'] == 200
        assert resp['headers']['foo'] == 'bar'

    app = s.web.server([('/', {'GET': handler})])
    with s.web.test(app) as url:
        s.async.run_sync(lambda: main(url))


def test_post():
    pass


def test_post_timeout():
    pass


def test_basic():
    @s.async.coroutine
    def handler(request):
        yield s.async.moment
        assert request['verb'] == 'get'
        raise s.async.Return({'headers': {'foo': 'bar'},
                              'code': 200,
                              'body': 'ok'})
    app = s.web.server([('/', {'GET': handler})])
    # TODO use s.web.get and a coroutine and drop requests
    # make a s.web.get_sync func
    with s.web.test(app) as url:
        resp = requests.get(url)
        assert resp.text == 'ok'
        assert resp.headers['foo'] == 'bar'


def test_middleware():
    def middleware(old_handler):
        @s.async.coroutine
        def new_handler(request):
            request = s.dicts.merge(request, {'headers': {'asdf': ' [mod req]'}})
            response = yield old_handler(request)
            response = s.dicts.merge(response, {'body': response['body'] + ' [mod resp]'})
            raise s.async.Return(response)
        return new_handler
    @middleware
    @s.async.coroutine
    def handler(request):
        yield s.async.moment
        raise s.async.Return({'headers': {'foo': 'bar'},
                              'code': 200,
                              'body': 'ok' + request['headers']['asdf']})
    app = s.web.server([('/', {'GET': handler})])
    with s.web.test(app) as url:
        resp = requests.get(url)
        assert resp.text == 'ok [mod req] [mod resp]'


def test_url_params():
    @s.async.coroutine
    def handler(request):
        yield s.async.moment
        raise s.async.Return({'code': 200,
                              'body': json.dumps(request['query'])})
    app = s.web.server([('/', {'GET': handler})])
    with s.web.test(app) as url:
        resp = requests.get(url + '?asdf=123&foo=bar&foo=notbar&stuff')
        assert resp.json() == {'asdf': '123',
                               'foo': ['bar', 'notbar'],
                               'stuff': ''}


def test_url_args():
    @s.async.coroutine
    def handler(request):
        yield s.async.moment
        raise s.async.Return({'code': 200,
                              'body': json.dumps({'foo': request['arguments']['foo']})})
    app = s.web.server([('/:foo/stuff', {'GET': handler})])
    with s.web.test(app) as url:
        resp = requests.get(url + 'something/stuff')
        assert resp.json() == {'foo': 'something'}

@@ -75,24 +75,8 @@
 t s%0A
-import requests%0A
 impo
@@ -1450,100 +1450,8 @@
 )%5D)%0A
-    # TODO use s.web.get and a coroutine and drop requests%0A    # make a s.web.get_sync func%0A
     
@@ -1490,36 +1490,38 @@
      resp = 
-requests.get
+s.web.get_sync
 (url)%0A      
@@ -1529,29 +1529,32 @@
  assert resp
-.text
+%5B'body'%5D
  == 'ok'%0A   
@@ -1573,16 +1573,19 @@
 resp
-.
+%5B'
 headers
+'%5D
 %5B'fo
@@ -2371,36 +2371,38 @@
      resp = 
-requests.get
+s.web.get_sync
 (url)%0A      
@@ -2418,13 +2418,16 @@
 resp
-.text
+%5B'body'%5D
  == 
@@ -2761,36 +2761,38 @@
      resp = 
-requests.get
+s.web.get_sync
 (url + '?asd
@@ -2832,34 +2832,67 @@
         
-assert resp.json()
+data = json.loads(resp%5B'body'%5D)%0A        assert data
  == %7B'as
@@ -2903,23 +2903,16 @@
  '123',%0A
-       
         
@@ -2953,23 +2953,16 @@
 tbar'%5D,%0A
-       
         
@@ -3335,20 +3335,22 @@
 p = 
-requests.get
+s.web.get_sync
 (url
@@ -3390,18 +3390,31 @@
 ert 
-resp.json(
+json.loads(resp%5B'body'%5D
 ) ==

use s.web.get_sync instead of requests