Skip to content

Commit

Permalink
feat(server): support non-gzipped requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Feb 16, 2025
1 parent 17eb5d5 commit e24366a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
9 changes: 7 additions & 2 deletions fakesnow/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ async def login_request(request: Request) -> JSONResponse:
database = request.query_params.get("databaseName")
schema = request.query_params.get("schemaName")
body = await request.body()
body_json = json.loads(gzip.decompress(body))
if request.headers.get("Content-Encoding") == "gzip":
body = gzip.decompress(body)
body_json = json.loads(body)
session_params: dict[str, Any] = body_json["data"]["SESSION_PARAMETERS"]
if db_path := session_params.get("FAKESNOW_DB_PATH"):
# isolated creates a new in-memory database, rather than using the shared in-memory database
Expand All @@ -53,7 +55,10 @@ async def query_request(request: Request) -> JSONResponse:
conn = to_conn(request)

body = await request.body()
body_json = json.loads(gzip.decompress(body))
if request.headers.get("Content-Encoding") == "gzip":
body = gzip.decompress(body)

body_json = json.loads(body)

sql_text = body_json["sqlText"]

Expand Down
59 changes: 59 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pytest
import pytz
import requests
import snowflake.connector
from dirty_equals import IsUUID
from snowflake.connector.cursor import ResultMetadata
Expand Down Expand Up @@ -110,6 +111,64 @@ def test_server_abort_request(server: dict) -> None:
cur.execute("select 'will abort'")


def test_server_no_gzip(server: dict) -> None:
# mimic the go snowflake connector which does not gzip requests
headers = {
"Accept": "application/snowflake",
"Content-Type": "application/json",
"Accept-Encoding": "gzip",
"User-Agent": "Go/1.13.0 (darwin-arm64) gc/go1.23.6",
"Client_App_Id": "Go",
"Client_App_Version": "1.13.0",
}

login_payload = {
"data": {
"CLIENT_APP_ID": "Go",
"CLIENT_APP_VERSION": "1.13.0",
"SVN_REVISION": "",
"ACCOUNT_NAME": "fakesnow",
"LOGIN_NAME": "fake",
"PASSWORD": "snow",
"SESSION_PARAMETERS": {"CLIENT_VALIDATE_DEFAULT_PARAMETERS": True},
"CLIENT_ENVIRONMENT": {
"APPLICATION": "Go",
"OS": "darwin",
"OS_VERSION": "gc-arm64",
"OCSP_MODE": "FAIL_OPEN",
"GO_VERSION": "go1.23.6",
},
}
}

response = requests.post(
f"http://{server['host']}:{server['port']}/session/v1/#-request",
headers=headers,
json=login_payload,
timeout=5,
)
assert response.status_code == 200
assert response.json()["success"]
token = response.json()["data"]["token"]

payload = {
"sqlText": "SELECT current_timestamp() as TIME, current_user() as USER, current_role() as ROLE;",
"asyncExec": False,
"sequenceId": 1,
"isInternal": False,
"queryContextDTO": {},
}

response = requests.post(
f"http://{server['host']}:{server['port']}/queries/v1/query-request?requestId=uuid1&request_guid=uuid2",
headers=headers | {"Authorization": f'Snowflake Token="{token}"'},
json=payload,
timeout=5,
)
assert response.status_code == 200
assert response.json()["success"]


def test_server_errors(scur: snowflake.connector.cursor.SnowflakeCursor) -> None:
cur = scur
with pytest.raises(snowflake.connector.errors.ProgrammingError) as excinfo:
Expand Down

0 comments on commit e24366a

Please # to comment.