mirror of
https://github.com/github/codeql.git
synced 2026-04-30 03:05:15 +02:00
Python: Add proper HTTP response tests for Tornado
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import python
|
||||
import experimental.meta.ConceptsTest
|
||||
//
|
||||
// class DedicatedResponseTest extends HttpServerHttpResponseTest {
|
||||
// DedicatedResponseTest() { file.getShortName() = "response_test.py" }
|
||||
// }
|
||||
//
|
||||
// class OtherResponseTest extends HttpServerHttpResponseTest {
|
||||
// OtherResponseTest() { not this instanceof DedicatedResponseTest }
|
||||
//
|
||||
// override string getARelevantTag() { result = "HttpResponse" }
|
||||
// }
|
||||
|
||||
class DedicatedResponseTest extends HttpServerHttpResponseTest {
|
||||
DedicatedResponseTest() { file.getShortName() = "response_test.py" }
|
||||
}
|
||||
|
||||
class OtherResponseTest extends HttpServerHttpResponseTest {
|
||||
OtherResponseTest() { not this instanceof DedicatedResponseTest }
|
||||
|
||||
override string getARelevantTag() { result = "HttpResponse" }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import tornado.web
|
||||
import tornado.httputil
|
||||
|
||||
|
||||
class ResponseWriting(tornado.web.RequestHandler):
|
||||
def get(self, type_): # $ requestHandler routedParameter=type_
|
||||
if type_ == "str":
|
||||
self.write("foo") # $ MISSING: HttpResponse mimetype=text/html responseBody="foo"
|
||||
elif type_ == "bytes":
|
||||
self.write(b"foo") # $ MISSING: HttpResponse mimetype=text/html responseBody=b"foo"
|
||||
elif type_ == "dict":
|
||||
d = {"foo": 42}
|
||||
# Content-type will be set to `application/json`
|
||||
self.write(d) # $ MISSING: HttpResponse mimetype=application/json responseBody=d
|
||||
else:
|
||||
raise Exception("Bad type {} {}".format(type_, type(type_)))
|
||||
|
||||
|
||||
class ExplicitContentType(tornado.web.RequestHandler):
|
||||
def get(self): # $ requestHandler
|
||||
# Note: Our current modeling makes it quite hard to give a good annotation here
|
||||
# this write is technically while the HTTP response has mimetype text/html, but
|
||||
# the returned HTTP response will have mimetype text/plain, which is _really_
|
||||
# what matters.
|
||||
|
||||
self.write("foo") # $ MISSING: HttpResponse mimetype=text/html responseBody=b"foo"
|
||||
self.set_header("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
def post(self): # $ requestHandler
|
||||
self.set_header("Content-Type", "text/plain; charset=utf-8")
|
||||
self.write("foo") # $ MISSING: HttpResponse mimetype=text/plain responseBody=b"foo"
|
||||
|
||||
|
||||
class ExampleRedirect(tornado.web.RequestHandler):
|
||||
def get(self): # $ requestHandler
|
||||
self.redirect("http://example.com") # TODO: Model redirect
|
||||
|
||||
|
||||
class ExampleConnectionWrite(tornado.web.RequestHandler):
|
||||
def get(self, stream=False): # $ requestHandler routedParameter=stream
|
||||
|
||||
if not stream:
|
||||
self.request.connection.write_headers(
|
||||
tornado.httputil.ResponseStartLine('', 200, 'OK'),
|
||||
tornado.httputil.HTTPHeaders(),
|
||||
)
|
||||
self.request.connection.write(b"foo") # $ MISSING: HttpResponse responseBody="foo"
|
||||
self.request.connection.finish()
|
||||
else:
|
||||
# Note: The documentation says that connection.detach(): "May only be called
|
||||
# during HTTPMessageDelegate.headers_received". Doing it here actually
|
||||
# works, but does make tornado spit out some errors... good enough to
|
||||
# illustrate the behavior.
|
||||
#
|
||||
# https://www.tornadoweb.org/en/stable/http1connection.html#tornado.http1connection.HTTP1Connection.detach
|
||||
stream = self.request.connection.detach()
|
||||
stream.write(b"foo stream") # $ MISSING: HttpResponse responseBody="foo stream"
|
||||
stream.close()
|
||||
|
||||
def make_app():
|
||||
return tornado.web.Application(
|
||||
[
|
||||
(r"/ResponseWriting/(str|bytes|dict)", ResponseWriting), # $ routeSetup="/ResponseWriting/(str|bytes|dict)"
|
||||
(r"/ExplicitContentType", ExplicitContentType), # $ routeSetup="/ExplicitContentType"
|
||||
(r"/ExampleRedirect", ExampleRedirect), # $ routeSetup="/ExampleRedirect"
|
||||
(r"/ExampleConnectionWrite", ExampleConnectionWrite), # $ routeSetup="/ExampleConnectionWrite"
|
||||
(r"/ExampleConnectionWrite/(stream)", ExampleConnectionWrite), # $ routeSetup="/ExampleConnectionWrite/(stream)"
|
||||
],
|
||||
debug=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import tornado.ioloop
|
||||
|
||||
app = make_app()
|
||||
app.listen(8888)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
||||
# http://localhost:8888/ResponseWriting/str
|
||||
# http://localhost:8888/ResponseWriting/bytes
|
||||
# http://localhost:8888/ResponseWriting/dict
|
||||
# http://localhost:8888/ExplicitContentType
|
||||
# http://localhost:8888/ExampleRedirect
|
||||
# http://localhost:8888/ExampleConnectionWrite
|
||||
@@ -1,35 +0,0 @@
|
||||
import tornado.web
|
||||
|
||||
|
||||
class ResponseWriting(tornado.web.RequestHandler):
|
||||
def get(self, type_): # $ requestHandler routedParameter=type_
|
||||
if type_ == "str":
|
||||
self.write("foo")
|
||||
elif type_ == "bytes":
|
||||
self.write(b"foo")
|
||||
elif type_ == "dict":
|
||||
# Content-type will be set to `application/json`
|
||||
self.write({"foo": 42})
|
||||
else:
|
||||
raise Exception("Bad type {} {}".format(type_, type(type_)))
|
||||
|
||||
|
||||
def make_app():
|
||||
return tornado.web.Application(
|
||||
[
|
||||
(r"/ResponseWriting/(str|bytes|dict)", ResponseWriting), # $ routeSetup="/ResponseWriting/(str|bytes|dict)"
|
||||
],
|
||||
debug=True,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import tornado.ioloop
|
||||
|
||||
app = make_app()
|
||||
app.listen(8888)
|
||||
tornado.ioloop.IOLoop.current().start()
|
||||
|
||||
# http://localhost:8888/ResponseWriting/str
|
||||
# http://localhost:8888/ResponseWriting/bytes
|
||||
# http://localhost:8888/ResponseWriting/dict
|
||||
Reference in New Issue
Block a user