def testCustomClient(self): pipeline = client.client_pipeline(logging=True, log_level="DEBUG") myclient = client.Client(pipeline=pipeline, assert_=testing.assert_status_code._200) payload = {'key1': 'value1', 'key2': 'value2'} myclient.get("http://httpbin.org/get", query_string=payload) myclient.head("http://httpbin.org/get", query_string=payload) payload = {'key1': 'value1', 'key2': 'value2'} myclient.post("http://httpbin.org/post", post=payload) payload = {'key1': 'value1', 'key2': 'value2'} myclient.post("http://httpbin.org/post", post=json.dumps(payload)) payload = {'key1': 'value1', 'key2': 'value2'} myclient.put("http://httpbin.org/put", post=json.dumps(payload)) payload = {'key1': 'value1', 'key2': 'value2'} myclient.delete("http://httpbin.org/delete", post=json.dumps(payload)) myclient.post("http://httpbin.org/post", headers={"content-type": "application/json"}, post=json.dumps(payload)) myclient.get("http://httpbin.org/gzip") try: myclient.get("http://no") self.fail("should have raised an exception") except: log.debug("yay I got an error", exc_info=True) myclient.get("http://no", assert_=testing.assert_status_code._400)
def testCustomClient(self): pipeline = client.client_pipeline(logging=True, log_level="DEBUG") myclient = client.Client(pipeline=pipeline, assert_=testing.assert_status_code._200) payload = {'key1': 'value1', 'key2': 'value2'} myclient.get("http://httpbin.org/get", query_string=payload) myclient.head("http://httpbin.org/get", query_string=payload) payload = {'key1': 'value1', 'key2': 'value2'} myclient.post("http://httpbin.org/post", post=payload) payload = {'key1': 'value1', 'key2': 'value2'} myclient.post("http://httpbin.org/post", post=json.dumps(payload)) payload = {'key1': 'value1', 'key2': 'value2'} myclient.put("http://httpbin.org/put", post=json.dumps(payload)) payload = {'key1': 'value1', 'key2': 'value2'} myclient.delete("http://httpbin.org/delete", post=json.dumps(payload)) myclient.post("http://httpbin.org/post", headers={"content-type": "application/json"}, post=json.dumps(payload)) myclient.get("http://httpbin.org/gzip") try: myclient.get("http://no") self.fail("should have raised an exception") except: log.debug("yay I got an error", exc_info=True)
def __init__(self, pipeline=None, redirect=False): app = pipeline or client_pipeline() if redirect: app = auto_redirect_filter(app) else: pass Client.__init__(self, pipeline=app, assert_=_200_or_302)
def testInvalidLogLevel(self): """ should raise value error """ try: filters.http_log_filter(client.client_pipeline(), "NO") self.fail("no error") except ValueError: pass
def test_cookie_identity_policy(): config = setup() app = morepath.App(testing_config=config) @app.path(path='{id}') class Model(object): def __init__(self, id): self.id = id class Permission(object): pass @app.permission(model=Model, permission=Permission) def get_permission(identity, model, permission): return identity.userid == 'user' @app.view(model=Model, permission=Permission) def default(self, request): return "Model: %s" % self.id @app.view(model=Model, name='log_in') def log_in(self, request): response = Response() generic.remember(response, request, Identity(userid='user', payload='Amazing'), lookup=request.lookup) return response @app.view(model=Model, name='log_out') def log_out(self, request): response = Response() generic.forget(response, request, lookup=request.lookup) return response @app.identity_policy() def policy(): return DumbCookieIdentityPolicy() config.commit() c = Client(client_pipeline(app, cookie_support=True)) response = c.get('/foo') assert response.status == '401 Unauthorized' response = c.get('/foo/log_in') response = c.get('/foo') assert response.status == '200 OK' assert response.body == 'Model: foo' response = c.get('/foo/log_out') response = c.get('/foo') assert response.status == '401 Unauthorized'
""" enable logging of request and response """ from webobtoolkit import client import logging logging.basicConfig(level=logging.DEBUG) c = client.Client(client.client_pipeline(logging=True, log_level=logging.DEBUG)) c.get("http://google.com")
""" getting a response from a wsgi application """ from webobtoolkit import client def application(environ, start_response): """ most python webframeworks provide a way to expose your web application as a WSGI app. consult your framework documentation for details. """ status = "200 OK" # HTTP messages have a status body = "Hello World" # HTTP messages have a body # HTTP messages have headers to describe various things, at a # minimum describing the type(Content-Type) and the length of the # content(Content-Length) headers = [("Content-Type", "text/plain"), ("Content-Length", str(len(body)))] start_response(status, headers) # calling the function passed in # with the status and headers of # the HTTP Response Message return [body] # returning a list containing the body of the HTTP # Response Message print client.Client(pipeline=client.client_pipeline(application)).get("/")
""" uploading files example """ from webobtoolkit.client import Client, client_pipeline from webob import Request, Response def application(environ, start_response): """this application merely spits out the keys of the form that was posted. we are using webob Request and Response for brevity """ request = Request(environ) return Response(str(request.POST.keys()))(environ, start_response) client = Client(pipeline=client_pipeline(application)) print client.post("/", files=dict(file1=("myfile.txt", "this is a file containing this text")))
""" example client usage """ from webobtoolkit.client import Client, client_pipeline from webobtoolkit.proxy import send_request_app import logging logging.basicConfig(level="DEBUG") # first we make an pipeline pipeline = client_pipeline(wsgi=send_request_app, # this wsgi app sends the request to the url you specify cookie_support=True, # turn on cookie support content_decoding=True, # decompress responses if necessary logging=True, # turn on logging log_level="DEBUG") # set log level client = Client(pipeline=pipeline) response = client.get("http://www.google.com", query_string=(dict(q="wsgi as http client"))) assert response.status_int == 200, "something went wrong"
def testbasic_app_init(self): """ enable logging with no log_level """ client.client_pipeline(logging=True)