def setUp(self):
     self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT)
     self.query_lat = D('37.8016')
     self.query_lon = D('-122.4783')
class ClientTest(unittest.TestCase):
    def setUp(self):
        self.client = Client(MY_OAUTH_KEY, MY_OAUTH_SECRET, API_VERSION, API_HOST, API_PORT)
        self.query_lat = D('37.8016')
        self.query_lon = D('-122.4783')

    def test_wrong_endpoint(self):
        self.assertRaises(Exception, self.client._endpoint, 'wrongwrong')

    def test_missing_argument(self):
        self.assertRaises(Exception, self.client._endpoint, 'feature')

    def test_get_point_feature(self):
        mockagent = MockAgent(FakeSuccessResponse([EXAMPLE_POINT_BODY], {'status': '200', 'content-type': 'application/json', 'thingie': "just to see if you're listening"}))
        self.client.agent = mockagent

        d = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")
        def check_res(res):
            self.assertEqual(mockagent.method, 'GET')
            self.assertEqual(mockagent.endpoint, 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970"))
            # the code under test is required to have json-decoded this before handing it back
            self.failUnless(isinstance(res, Feature), (repr(res), type(res)))
            self.failUnlessEqual(res._http_response.headers, {'status': '200', 'content-type': 'application/json', 'thingie': "just to see if you're listening"})

        d.addCallback(check_res)
        return d

    def test_get_polygon_feature(self):
        mockagent = MockAgent(FakeSuccessResponse([EXAMPLE_BODY], {'status': '200', 'content-type': 'application/json'}))
        self.client.agent = mockagent

        d = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")
        def check_res(res):
            self.assertEqual(mockagent.method, 'GET')
            self.assertEqual(mockagent.endpoint, 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970"))
            # the code under test is required to have json-decoded this before handing it back
            self.failUnless(isinstance(res, Feature), (repr(res), type(res)))

        d.addCallback(check_res)
        return d

    def test_type_check_request(self):
        self.failUnlessRaises(TypeError, self.client._request, 'whatever', 'POST', {'bogus': "non string"})

    def test_get_feature_bad_json(self):
        mockagent = MockAgent(FakeSuccessResponse([EXAMPLE_BODY, 'some crap'], {'status': '200', 'content-type': 'application/json'}))
        self.client.agent = mockagent

        d = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")
        self.failUnlessFailure(d, DecodeError)
        def after_error(f):
            self.assertEqual(mockagent.endpoint, 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970"))
            self.assertEqual(mockagent.method, 'GET')

        d.addCallback(after_error)

        return d

    def test_dont_json_decode_results(self):
        """ _request() is required to return a deferred that fires
        with a Response that has a deliverBody that delivers the exact
        string that the HTTP server sent to it -- no transforming it,
        such as by json-decoding. """

        mockagent = MockAgent(FakeSuccessResponse(['{ "Hello": "I am a string. \xe2\x9d\xa4" }'.decode('utf-8')], {'status': '200', 'content-type': 'application/json'}))
        self.client.agent = mockagent

        d = self.client._request("http://thing", 'POST')
        d.addCallback(get_body)
        def _with_body(body):
            self.failUnlessEqual(body, '{ "Hello": "I am a string. \xe2\x9d\xa4" }'.decode('utf-8'))
        d.addCallback(_with_body)
        return d

    def test_dont_Recordify_results(self):
        """ _request() is required to return the exact string that the HTTP
        server sent to it -- no transforming it, such as by json-decoding and
        then constructing a Record. """

        EXAMPLE_RECORD_JSONSTR=json.dumps({ 'geometry' : { 'type' : 'Point', 'coordinates' : [D('10.0'), D('11.0')] }, 'id' : 'my_id', 'type' : 'Feature', 'properties' : { 'key' : 'value'  , 'type' : 'object' } })

        mockagent = MockAgent(FakeSuccessResponse([EXAMPLE_RECORD_JSONSTR], {'status': '200', 'content-type': 'application/json'}))
        self.client.agent = mockagent

        d = self.client._request("http://thing", 'POST')
        d.addCallback(get_body)
        def check(res):
            self.failUnlessEqual(res, EXAMPLE_RECORD_JSONSTR)
        d.addCallback(check)
        return d

    def test_get_feature_error(self):
        fakeresp = FakeResponse('{"message": "help my web server is confuzzled"}', {'status': '500', 'content-type': 'application/json'}, code=500)
        mockagent = MockAgent(fakeresp)
        self.client.agent = mockagent

        d = self.client.get_feature("SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970")
        self.failUnlessFailure(d, FakeResponse)
        def after_error(f):
            self.failUnlessEqual(f.code, 500)
            self.assertEqual(mockagent.endpoint, 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, "SG_4bgzicKFmP89tQFGLGZYy0_34.714646_-86.584970"))
            self.assertEqual(mockagent.method, 'GET')
        d.addCallback(after_error)
        return d