Skip to content

Commit

Permalink
Properly decode base64 body
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttrinh committed Oct 2, 2024
1 parent 19c26dd commit feca4c9
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
5 changes: 5 additions & 0 deletions edb/server/net_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import asyncio
import logging
import httpx
import base64

from edb.ir import statypes
from edb.server import defines
Expand Down Expand Up @@ -134,6 +135,10 @@ class ScheduledRequest:
body: typing.Optional[bytes]
headers: typing.Optional[list[dict]]

def __post_init__(self):
if self.body is not None:
self.body = base64.b64decode(self.body).decode('utf-8').encode()


async def handle_request(
client: httpx.AsyncClient, db: dbview.Database, request: ScheduledRequest
Expand Down
83 changes: 79 additions & 4 deletions tests/test_http_std_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ def tearDown(self):
self.mock_server.stop()
self.mock_server = None

async def test_http_std_net_con_send_request(self):
async def test_http_std_net_con_schedule_request_get_01(self):
assert self.mock_server is not None

example_request = (
'GET',
self.base_url,
'/test',
'/test-get-01',
)
url = f"{example_request[1]}{example_request[2]}"
self.mock_server.register_route_handler(*example_request)(
(
json.dumps(
Expand Down Expand Up @@ -80,7 +81,7 @@ async def test_http_std_net_con_send_request(self):
)
select request {*};
""",
url=f"{self.base_url}/test",
url=url,
)

async for tr in self.try_until_succeeds(
Expand All @@ -99,11 +100,85 @@ async def test_http_std_net_con_send_request(self):
))
select request {*};
""",
url=f"{self.base_url}/test",
url=url,
)

requests_for_example = self.mock_server.requests[example_request]
self.assertEqual(len(requests_for_example), 1)
headers = list(requests_for_example[0]["headers"].items())
self.assertIn(("accept", "text/plain"), headers)
self.assertIn(("x-test-header", "test-value"), headers)

async def test_http_std_net_con_schedule_request_post_01(self):
assert self.mock_server is not None

example_request = (
'POST',
self.base_url,
'/test-post-01',
)
url = f"{example_request[1]}{example_request[2]}"
self.mock_server.register_route_handler(*example_request)(
(
json.dumps(
{
"message": "Hello, world!",
}
),
200,
{"Content-Type": "application/json"},
)
)

await self.con.query(
"""
with
nh as module std::net::http,
net as module std::net,
url := <str>$url,
body := <bytes>$body,
request := (
insert nh::ScheduledRequest {
created_at := datetime_of_statement(),
state := std::net::RequestState.Pending,
url := url,
method := nh::Method.POST,
headers := [
("Accept", "text/plain"),
("x-test-header", "test-value"),
],
body := body,
}
)
select request {*};
""",
url=url,
body=b"Hello, world!",
)

async for tr in self.try_until_succeeds(
delay=2, timeout=120, ignore=(edgedb.CardinalityViolationError,)
):
async with tr:
await self.con.query(
"""
with
url := <str>$url,
request := assert_exists((
select std::net::http::ScheduledRequest
filter .url = url
and .state != std::net::RequestState.Pending
limit 1
))
select request {*};
""",
url=url,
)

requests_for_example = self.mock_server.requests[example_request]
self.assertEqual(len(requests_for_example), 1)
headers = list(requests_for_example[0]["headers"].items())
self.assertIn(("accept", "text/plain"), headers)
self.assertIn(("x-test-header", "test-value"), headers)
self.assertEqual(requests_for_example[0]["body"], "Hello, world!")

0 comments on commit feca4c9

Please sign in to comment.