|
4 | 4 | from subprocess import Popen, PIPE
|
5 | 5 | from unittest import TestCase, main
|
6 | 6 | from pathlib import Path
|
| 7 | +import base64 |
| 8 | +import json |
7 | 9 | import time
|
8 | 10 | import os
|
9 | 11 | import requests
|
@@ -62,12 +64,14 @@ def run_command(self, cmd):
|
62 | 64 |
|
63 | 65 | def sleep_1s(self):
|
64 | 66 | time.sleep(SLEEP_TIME)
|
65 |
| - |
66 |
| - def invoke_function(self): |
| 67 | + |
| 68 | + def invoke_function(self, json={}, headers={}): |
67 | 69 | return requests.post(
|
68 |
| - f"http://localhost:{self.PORT}/2015-03-31/functions/function/invocations", json={} |
| 70 | + f"http://localhost:{self.PORT}/2015-03-31/functions/function/invocations", |
| 71 | + json=json, |
| 72 | + headers=headers, |
69 | 73 | )
|
70 |
| - |
| 74 | + |
71 | 75 | @contextmanager
|
72 | 76 | def create_container(self, param, image):
|
73 | 77 | try:
|
@@ -234,6 +238,24 @@ def test_port_override(self):
|
234 | 238 | self.assertEqual(b'"My lambda ran succesfully"', r.content)
|
235 | 239 |
|
236 | 240 |
|
| 241 | + def test_custom_client_context(self): |
| 242 | + image, rie, image_name = self.tagged_name("custom_client_context") |
| 243 | + |
| 244 | + params = f"--name {image} -d -v {self.path_to_binary}:/local-lambda-runtime-server -p {self.PORT}:8080 --entrypoint /local-lambda-runtime-server/{rie} {image_name} {DEFAULT_1P_ENTRYPOINT} main.custom_client_context_handler" |
| 245 | + |
| 246 | + with self.create_container(params, image): |
| 247 | + r = self.invoke_function(headers={ |
| 248 | + "X-Amz-Client-Context": base64.b64encode(json.dumps({ |
| 249 | + "custom": { |
| 250 | + "foo": "bar", |
| 251 | + "baz": 123, |
| 252 | + } |
| 253 | + }).encode('utf8')).decode('utf8'), |
| 254 | + }) |
| 255 | + content = json.loads(r.content) |
| 256 | + self.assertEqual("bar", content["foo"]) |
| 257 | + self.assertEqual(123, content["baz"]) |
| 258 | + |
237 | 259 |
|
238 | 260 | if __name__ == "__main__":
|
239 | 261 | main()
|
0 commit comments