Ejemplo n.º 1
0
    def test_nonroot_mismatchURLs(self):
        ''' Tests that URLs of a non root base URL handle mismatches
        This is when the passed in URL is for a root folder that is not the
        same as the base subfolder of the application.
        '''
        self.app = Restler("http://nope/test/")
        params = {"url": "/derp"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "/derp")
Ejemplo n.º 2
0
    def test_nonroot_URLs(self):
        ''' Test that URLs of a non root base URL are handled properly
        This is a test for when an app uses a path in the base URL (i.e.
        something like `http://twitter.com/api/`) so URLs that get converted
        take into account this additional base folder
        '''
        self.app = Restler("http://nope/test/")
        params = {"url": "/test"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "http://nope/test")
        # Bug, url in above doesn't have trailing slash, messed up with the natural
        # trailing slash
        self.app = Restler("http://nope/test/")
        params = {"url": "/test/"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "http://nope/test")
Ejemplo n.º 3
0
from restler import Restler

import code

try:
    import readline
except ImportError:
    pass
else:
    # import rlcompleter
    # readline.set_completer(rlcompleter.Completer(imported_objects).complete)
    readline.parse_and_bind("tab:complete")

lo = Restler('http://localhost:9000/')

code.interact('''
preimported: Restler
predefined: lo -> Restler('localhost')

''',
              local=locals())
Ejemplo n.º 4
0
def build(token, key):
    handler = Restler("https://api.trello.com/1/")
    handler._route._default_params["token"] = token
    handler._route._default_params["key"] = key

    return handler
Ejemplo n.º 5
0
class TestRequest(unittest.TestCase):
    def setUp(self):
        self.app = Restler("http://127.0.0.1/")
        self.app.__test__ = True

    def assertSameUrlStrings(self, a, b):
        a_set = a.split("&")
        b_set = b.split("&")
        if hasattr(self, "assertItemsEqual"):
            return self.assertItemsEqual(a_set, b_set)
        else:
            return self.assertCountEqual(a_set, b_set)

    def test_basic(self):
        ''' Tests the basic Request formation from a `Route`
        Gets the created `urllib2.Request` object and confirms basic setup
        '''
        request = self.app.users()
        self.assertEqual(request.get_selector(), "/users")

    def test_methods(self):
        ''' Tests the method and data setup for a `Request`
        Checks the created Request object for method setup and data creation
        '''
        request = self.app.users("POST", user="******")
        self.assertEqual(request.get_method(), "POST")
        self.assertEqual(normalize(request.get_data()), "user=test")

    def test_method_casing(self):
        ''' Tests the casing of the method
        Makes sure the method gets cast to all uppercase
        '''
        request = self.app.users("post")
        self.assertEqual(request.get_method(), "POST")

    def test_headers(self):
        ''' Tests setting a header for a request
        Checks that you can set headers via the request call
        '''
        request = self.app.users(headers={"Accepts": "application/json"})
        self.assertTrue(request.has_header("Accepts"))

    def test_json(self):
        ''' Tests the data body respects the content-type
        Sets the `Content-type` header for json, the data body should use this
        mimetype
        '''
        request = self.app.users(headers={"Content-type": "application/json"},
                                 users="test", foo="bar")
        self.assertEqual(json.dumps({"users": "test", "foo": "bar"}),
                         normalize(request.get_data()))

    def test_long_data(self):
        ''' Tests the data body for lots of data
        Creates a request with a large set of data, checks that it all gets
        added to the data body
        '''
        request = self.app.users(users="test", foo="bar", bar="baz")
        self.assertSameUrlStrings("foo=bar&bar=baz&users=test",
                                  normalize(request.get_data()))

    def test_qs_path(self):
        ''' Tests that a path with a query string sets the params
        If a path with a query string is fed into a `Route`, the query string
        should be parsed and set as default params
        '''
        route = self.app['users?name=test']
        request = route("POST")
        self.assertEqual("name=test", normalize(request.get_data()))
        self.assertEqual(request.get_selector(), "/users")

    def test_multi_params(self):
        ''' Tests a data body with an array value
        Creates a request with multiple values for a key set, checks that the
        key is used for each value individually.
        '''
        request = self.app.users(users=["foo", "bar", "baz"])
        self.assertSameUrlStrings("users=foo&users=bar&users=baz",
                                  normalize(request.get_data()))

    def test_get_data(self):
        ''' Tests that data for a GET is in the query string
        The data will also be in the header, but it is common for the data to
        live in the query string of the URL.
        '''
        route = self.app.test
        request = route(foo="bar")
        self.assertEqual("foo=bar", normalize(request.get_data()))
        self.assertEqual("/test?foo=bar", normalize(request.get_selector()))
Ejemplo n.º 6
0
 def setUp(self):
     self.app = Restler("http://127.0.0.1/")
     self.app.__test__ = True
Ejemplo n.º 7
0
 def setUp(self):
     self.local = Restler("http://127.0.0.1:9001")
Ejemplo n.º 8
0
import sys
try:
    import urllib2
except ImportError:
    import urllib.request as urllib2
from restler import Restler

JSON_TYPE = {"Content-Type": "application.json"}
FORM_ACCEPTS = {"Accepts": "application/x-www-form-urlencoded"}

app = Restler("http://127.0.0.1:9000")
# Basic call
try:
    response = app()
except urllib2.URLError as err:
    print(err)
    print("Make sure you have the `etc/http_server.py` script running")
    sys.exit(1)
print(response.data)
# Call with URL parameters
response = app.test(foo="bar")
print(response.data)
# POST with parameters
response = app.test("POST", foo="bar")
print(response.data)
# Set HTTP Headers
response = app.test(headers=JSON_TYPE, foo="bar")
print(response.data)
# Set the response mimetype in header
response = app.test(headers=FORM_ACCEPTS, foo="bar")
print(response.data)
Ejemplo n.º 9
0
 def setUp(self):
     self.app = Restler("http://127.0.0.1:9001")
Ejemplo n.º 10
0
def build(token, url="https://api.github.com/"):
    handler = Restler(url)
    handler._route._default_headers.append(("Authorization",
                                            "token " + token))

    return handler
Ejemplo n.º 11
0
 def setUp(self):
     self.app = Restler("http://nope/")