def test_forward_root(self): resp = Conn().get("/", req_hdrs={"Host": "not-example.com"}) status, hdrs, body = parse(resp) self.assertContains(status, "301 Moved Permanently") expect = "http://catchall.example.com/" self.assertEquals(hdrs["Location"], expect) self.assertContains(body, expect)
def test_forward_root(self): resp = self.get('/', req_hdrs={'Host': 'not-example.com'}) status, hdrs, body = parse(resp) self.assertContains(status, "301 Moved Permanently") expect = "http://catchall.example.com/" self.assertEqual(hdrs["Location"], expect) self.assertContains(body, expect)
def test_forward_relative(self): resp = self.get('/foo/bar', req_hdrs={'Host': 'secure.example.com'}) status, hdrs, body = parse(resp) self.assertContains(status, "301 Moved Permanently") expect = "https://www.example.com/secure/foo/bar" self.assertEqual(hdrs["Location"], expect) self.assertContains(body, expect)
def test_no_redirect(self): resp = self.get( '/', req_hdrs={ 'Host': 'example.com', 'X-Forwarded-Proto': 'https', # Already https. }) status, hdrs, body = parse(resp) self.assertContains(status, '200 OK')
def get_helper(self, idx): fn, content_type = self.files[idx] resp = Conn().get("/" + fn) status, hdrs, body = parse(resp) self.assertContains(status, "200 OK") self.assertEquals(hdrs["Accept-Ranges"], "bytes") self.assertEquals(hdrs["Content-Length"], str(self.datalen)) self.assertEquals(hdrs["Content-Type"], content_type) self.assertContains(hdrs["Server"], "darkhttpd/") self.assertEquals(body, self.data)
def test_https_redirect(self): resp = self.get('/foo/bar', req_hdrs={ 'Host': 'example.com', 'X-Forwarded-Proto': 'http', }) status, hdrs, body = parse(resp) self.assertContains(status, '301 Moved Permanently') expect = 'https://example.com/foo/bar' self.assertEqual(hdrs['Location'], expect) self.assertContains(body, expect)
def test_wrong_auth(self): resp = self.get( self.url, req_hdrs={ 'Authorization': 'Basic ' + base64.b64encode(b'myuser:wrongpass').decode('utf-8') }) status, hdrs, body = parse(resp) self.assertContains(status, '401 Unauthorized') self.assertEqual(hdrs['WWW-Authenticate'], 'Basic realm="User Visible Realm"') self.assertContains(hdrs['Server'], 'darkhttpd/')
def test_with_auth(self): resp = self.get(self.url, req_hdrs={ 'Authorization': 'Basic ' + base64.b64encode('myuser:mypass') }) status, hdrs, body = parse(resp) self.assertContains(status, '200 OK') self.assertEquals(hdrs["Accept-Ranges"], "bytes") self.assertEquals(hdrs["Content-Length"], str(self.datalen)) self.assertEquals(hdrs["Content-Type"], "image/jpeg") self.assertContains(hdrs["Server"], "darkhttpd/") assert body == self.data, [url, resp, status, hdrs, body] self.assertEquals(body, self.data)
from pprint import pprint import cparser, test testcode = """ #include <stdio.h> int main(int argc, char** argv) { printf("Hello %s\n", "world"); printf("args: %i\n", argc); int i; for(i = 0; i < argc; ++i) printf("%s\n", argv[i]); } """ state = test.parse(testcode, withGlobalIncludeWrappers=True) import interpreter interpreter = interpreter.Interpreter() interpreter.register(state) interpreter.registerFinalize() def dump(): for f in state.contentlist: if not isinstance(f, cparser.CFunc): continue if not f.body: continue print print "parsed content of " + str(f) + ":" for c in f.body.contentlist:
def test_no_server_id(self): resp = self.get('/', method='BOGUS') status, hdrs, body = parse(resp) self.assertContains(status, "400 Bad Request") self.assertFalse('Server' in hdrs) self.assertFalse(b'Generated by darkhttpd/' in body)
def test_no_listing(self): resp = self.get("/") status, hdrs, body = parse(resp) self.assertContains(status, "404 Not Found")
from pprint import pprint from cparser import * import test testcode = """ int16_t (*f)(); int16_t (*g)(char a, void*); int (*h); // ISO/IEC 9899:TC3 : C99 standard int fx(void), *fip(), (*pfi)(); // example 1, page 120 int (*apfi[3])(int *x, int *y); // example 2, page 120 int (*fpfi(int (*)(long), int))(int, ...); // example 3, page 120 """ state = test.parse(testcode) f = state.vars["f"] g = state.vars["g"] assert f.name == "f" assert isinstance(f.type, CFuncPointerDecl) assert f.type.type == CStdIntType("int16_t") assert f.type.args == [] assert isinstance(g.type, CFuncPointerDecl) gargs = g.type.args assert isinstance(gargs, list) assert len(gargs) == 2 assert isinstance(gargs[0], CFuncArgDecl) assert gargs[0].name == "a"
def test_no_auth(self): resp = self.get(self.url) status, hdrs, body = parse(resp) self.assertContains(status, '401 Unauthorized') self.assertEquals(hdrs['WWW-Authenticate'], 'Basic realm="User Visible Realm"')
def test_without_header(self): resp = self.get('/', req_hdrs={'Host': 'example.com'}) status, hdrs, body = parse(resp) self.assertContains(status, '200 OK')
def test_no_server_id(self): resp = Conn().get("/", method='BOGUS') status, hdrs, body = parse(resp) self.assertContains(status, "400 Bad Request") self.assertFalse(hdrs.has_key("Server")) self.assertFalse("Generated by darkhttpd/" in body)