serve-json-then-js.py (837B)
1 # Respond with valid JSON to the first request with the given key, 2 # and with valid JavaScript to the second. Used for testing scenarios where 3 # the same request URL results in different responses on subsequent requests. 4 def main(request, response): 5 try: 6 stash_key = request.GET.first(b"key") 7 8 run_count = request.server.stash.take(stash_key) 9 if not run_count: 10 run_count = 0 11 12 if run_count == 0: 13 response.headers.set(b"Content-Type", b"text/json") 14 response.content = '{"hello": "world"}' 15 else: 16 response.headers.set(b"Content-Type", b"application/javascript") 17 response.content = "export default 'hello';" 18 19 request.server.stash.put(stash_key, run_count + 1) 20 except: 21 response.set_error(400, u"Not enough parameters")