import flickrapi
import tests
import httplib as http_client
from flickr_harvester import FLICKR_HOST
from harvester.httplib_helper import wrap_api_call, parse_url
import unittest


@unittest.skipIf(tests.integration_env_available, "Skipping test since integration env is available.")
class HttpLibHelperTest(tests.TestCase):

    @unittest.skipIf(not tests.test_config_available, "Skipping test since test config not available.")
    def test_wrap_api_call(self):
        api = flickrapi.FlickrAPI(tests.FLICKR_KEY, tests.FLICKR_SECRET, store_token=False)
        request_record, response_record, raw_resp = wrap_api_call(
            lambda: api.photos.getInfo(photo_id="16796603565", secret="90f7d5c74c", format='json'),
            http_client.HTTPConnection,
            FLICKR_HOST
        )

        #Warc records
        self.assertEqual(request_record.type, "request")
        self.assertEqual(response_record.type, "response")

        #Raw resp
        self.assertTrue(raw_resp.startswith("""{"photo":{"id":"16796603565","secret":"90f7d5c74c","""))

        #Url
        self.assertEqual("https://api.flickr.com/services/rest/?nojsoncallback=1&secret=90f7d5c74c&photo_id="
                         "16796603565&method=flickr.photos.getInfo&format=json",
                         response_record.header["WARC-Target-URI"])

    def test_parse_url(self):
        self.assertEqual("/services/rest/?user_id=131866249&method=flickr.people.getPublicPhotos",
                         parse_url("POST /services/rest/?user_id=131866249&method=flickr.people.getPublicPhotos "
                                   "HTTP/1.1"))

        self.assertEqual("/services/rest/?user_id=131866249&method=flickr.people.getPublicPhotos",
                         parse_url("GET /services/rest/?user_id=131866249&method=flickr.people.getPublicPhotos "
                                   "HTTP/1.1"))

@@ -172,16 +172,31 @@
 nittest%0A
+import urlparse
 %0A%0A@unitt
@@ -1104,77 +1104,287 @@
 sert
-Equal(%22https://api.flickr.com/services/rest/?nojsoncallback=1&
+True(response_record.header%5B%22WARC-Target-URI%22%5D.startswith(%22https://api.flickr.com/services/rest/?%22))%0A        split_url = urlparse.urlparse(response_record.header%5B%22WARC-Target-URI%22%5D)%0A        self.assertDictEqual(%7B%22nojsoncallback%22: %5B%221%22%5D,%0A                              %22
 secret
-=
+%22: %5B%22
 90f7
@@ -1393,20 +1393,17 @@
 c74c
-&photo_id=%22%0A
+%22%5D,%0A     
     
@@ -1428,27 +1428,78 @@
    %22
-16796603565&
+photo_id%22: %5B%2216796603565%22%5D,%0A                              %22
 method
-=
+%22: %5B%22
 flic
@@ -1519,23 +1519,17 @@
 Info
-&format=json%22,%0A
+%22%5D,%0A     
     
@@ -1553,49 +1553,63 @@
     
-response_record.header%5B%22WARC-Target-URI%22%5D
+%22format%22: %5B%22json%22%5D%7D, urlparse.parse_qs(split_url.query)
 )%0A%0A 

Fix to test_wrap_api_call test to allow for variable ordering of querystring arguments.