-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from collections import namedtuple | ||
|
||
HTTP_GET = "GET" | ||
HTTP_POST = "POST" | ||
|
||
HttpStatus = namedtuple("HttpStatus", ["code", "msg"]) | ||
HTTP_OK = HttpStatus(200, "OK") | ||
HTTP_BAD_REQUEST = HttpStatus(403, "Bad-request") | ||
|
||
HTTP_1_0 = "HTTP/1.0" | ||
HTTP_1_1 = "HTTP/1.1" | ||
|
||
HTTP_METHODS = (HTTP_GET, HTTP_POST) | ||
HTTP_PROTOCOLS = (HTTP_1_0, HTTP_1_1) | ||
|
||
MIME_TEXT_PLAIN = "text/plain" | ||
|
||
CONTENT_TYPE = "Content-type" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
REQUEST_METHOD = "REQUEST_METHOD" | ||
SCRIPT_NAME = "SCRIPT_NAME" | ||
PATH_INFO = "PATH_INFO" | ||
QUERY_STRING = "QUERY_STRING" | ||
CONTENT_TYPE = "CONTENT_TYPE" | ||
CONTENT_LENGTH = "CONTENT_LENGTH" | ||
SERVER_NAME = "SERVER_NAME" | ||
SERVER_PORT = "SERVER_PORT" | ||
SERVER_PROTOCOL = "SERVER_PROTOCOL" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from clearest import POST, HTTP_BAD_REQUEST | ||
from tests.wsgi import WSGITestCase | ||
|
||
|
||
class Test(WSGITestCase): | ||
def test_application_bad_request(self): | ||
@POST("/asd") | ||
def asd(): | ||
return {} | ||
result = self.get("/asd") | ||
self.assertEqual(HTTP_BAD_REQUEST, self.status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from unittest import TestCase | ||
from clearest.http import * | ||
from clearest.wsgi import * | ||
from clearest.core import application | ||
|
||
|
||
class WSGITestCase(TestCase): | ||
def __init__(self, *args, **kwargs): | ||
super(WSGITestCase, self).__init__(*args, **kwargs) | ||
self.headers = [] | ||
self.status = None | ||
|
||
def _start_response(self, status, headers): | ||
code, msg = status.split(" ") | ||
self.status = HttpStatus(code=int(code), msg=msg) | ||
self.headers = headers | ||
|
||
def request(self, app, method, query): | ||
assert method in HTTP_METHODS | ||
env = {REQUEST_METHOD: method, | ||
SERVER_PROTOCOL: HTTP_1_1, | ||
SERVER_NAME: "wsgi_unit_test", | ||
SERVER_PORT: 42, | ||
SCRIPT_NAME: ""} | ||
parts = query.split("?") | ||
assert len(parts) <= 2 | ||
if len(parts) is 2: | ||
env[PATH_INFO], env[QUERY_STRING] = parts | ||
else: | ||
env[PATH_INFO] = query | ||
result = [] | ||
for data in app(env, self._start_response): | ||
result.append(data) | ||
return result | ||
|
||
def get(self, query, app=application): | ||
return self.request(app, HTTP_GET, query) |