"""
Test that make+request is making correct HTTP requests

Uses the awesome httpbin.org to validate responses
"""
import json
import twilio
from nose.tools import assert_equals
from nose.tools import raises
from mock import patch
from mock import Mock
from twilio import TwilioRestException
from twilio.rest.resources import make_request
from twilio.rest.resources import make_twilio_request

get_headers = {
    "User-Agent": "twilio-python/{}".format(twilio.__version__),
    "Accept": "application/json",
    }

post_headers = get_headers.copy()
post_headers["Content-Type"] = "application/x-www-form-urlencoded"

def test_get_params():
    tests = [
        ("http://httpbin.org/get", {"hey":"you", "foo":"bar"}),
        ("http://httpbin.org/get?hey=you", {"foo":"bar"}),
        ("http://httpbin.org/get?hey=you&foo=bar", {}),
        ]

    for test in tests:
        yield check_get_params, test[0], test[1]

def check_get_params(url, params):
    resp = make_request("GET", url, params=params)
    body = json.loads(resp.content)

    assert_equals(body["args"]["hey"], "you")
    assert_equals(body["args"]["foo"], "bar")

def test_resp_uri():
    resp = make_request("GET", "http://httpbin.org/get")
    assert_equals(resp.url, "http://httpbin.org/get")

@patch('twilio.rest.resources.make_request')
def test_make_twilio_request_headers(mock):
    url = "http://random/url"
    make_twilio_request("POST", url)
    mock.assert_called_with("POST", "http://random/url.json",
                            headers=post_headers)

@raises(TwilioRestException)
@patch('twilio.rest.resources.make_request')
def test_make_twilio_request_bad_data(mock):
    resp = Mock()
    resp.ok = False
    mock.return_value = resp

    url = "http://random/url"
    make_twilio_request("POST", url)
    mock.assert_called_with("POST", "http://random/url.json",
                            headers=post_headers)

@@ -615,118 +615,246 @@
 d%22%0A%0A
-def test_get_params():%0A    tests = %5B%0A   
+%0A@patch('twilio.rest.resources.Response')%0A@patch('httplib2.Http')%0Adef test_get_params(http_mock, response_mock):%0A    http = Mock()%0A
     
- (%22
 http
-://httpbin.org/get%22, %7B%22hey%22:%22you%22, %22foo%22:%22bar%22%7D),%0A        (
+.request.return_value = (Mock(), Mock())%0A    http_mock.return_value = http%0A    make_request(%22GET%22, 
 %22htt
@@ -876,43 +876,69 @@
 /get
-?hey=you%22, %7B%22foo%22:%22bar
+%22, params=%7B%22hey%22: %22you
 %22%7D)
-,
 %0A    
-    
+http.request.assert_called_with
 (%22ht
@@ -969,22 +969,16 @@
 =you
-&foo=bar%22, %7B%7D)
+%22, %22GET%22
 ,%0A  
@@ -987,431 +987,831 @@
     
-%5D%0A%0A
     
-for test in tests:%0A        yield check_get_params, test%5B0%5D, test%5B1%5D%0A%0Adef check_get
+body=None, headers=None)%0A%0A%0A@patch('twilio.rest.resources.Response')%0A@patch('httplib2.Http')%0Adef test_get_extra
 _para
+n
 ms(
-url, params
+http_mock, response_mock
 ):%0A    
-res
+htt
 p = 
-make_request(%22GET%22, url, params=params)%0A    body = json.loads(resp.content)%0A%0A    assert_equals(body%5B%22args%22%5D%5B
+Mock()%0A    http.request.return_value = (Mock(), Mock())%0A    http_mock.return_value = http%0A    make_request(%22GET%22, %22http://httpbin.org/get?foo=bar%22, params=%7B
 %22hey%22
-%5D,
+:
  %22you%22
+%7D
 )%0A    
-assert_equals(body%5B%22args%22%5D%5B%22foo%22%5D, %22bar%22)%0A%0Adef test_resp_uri():%0A    resp = make_request(%22GET%22, %22http://httpbin.org/get%22)%0A    assert_equals(resp.url, %22http://httpbin.org/get%22)
+http.request.assert_called_with(%22http://httpbin.org/get?foo=bar&hey=you%22, %22GET%22,%0A            body=None, headers=None)%0A%0A%0A@patch('twilio.rest.resources.Response')%0A@patch('httplib2.Http')%0Adef test_resp_uri(http_mock, response_mock):%0A    http = Mock()%0A    http.request.return_value = (Mock(), Mock())%0A    http_mock.return_value = http%0A    make_request(%22GET%22, %22http://httpbin.org/get%22)%0A    http.request.assert_called_with(%22http://httpbin.org/get%22, %22GET%22,%0A            body=None, headers=None)%0A
 %0A%0A@p

Remove httpbin.org testing dependency