Skip to content

Commit

Permalink
Add some actual tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi committed Jul 17, 2023
1 parent 2292ded commit 012199e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
sudo apt install -y python3-flask
- name: Checkout gmod-chttp
uses: actions/checkout@v3
with:
Expand Down Expand Up @@ -97,4 +102,7 @@ jobs:
- name: Run tests
working-directory: 'gmod-lua-runner-install'
run: |
"${GITHUB_WORKSPACE}/gmod-chttp/tests/server.py" 1>/dev/null 2>&1 &
until curl --silent "http://127.0.0.1:5000" 1>/dev/null 2>&1; do sleep 1; done
./runner test.lua
kill $!
63 changes: 63 additions & 0 deletions tests/chttp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,68 @@ return {
})
end
},
{
name = "Response has null byte in body",
async = true,
timeout = 1,
func = function()
CHTTP({
url = "http://127.0.0.1:5000/response_null_byte_in_body",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("Hello World\0!")
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Response has multiple cookies",
async = true,
timeout = 1,
func = function()
CHTTP({
url = "http://127.0.0.1:5000/response_multiple_cookies",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("Hello World!")

-- Header order is nondeterministic
expect((headers["Set-Cookie"] == "CookieA=1,CookieB=2") or (headers["Set-Cookie"] == "CookieB=2,CookieA=1")).to.beTrue()
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
{
name = "Response has multiple warnings",
async = true,
timeout = 1,
func = function()
CHTTP({
url = "http://127.0.0.1:5000/response_multiple_warning",
success = function(code, body, headers)
expect(code).to.equal(200)
expect(body).to.equal("Hello World!")

-- Header order is nondeterministic
expect((headers["Warning"] == "199 - Warning1,199 - Warning2") or (headers["Warning"] == "199 - Warning2,199 - Warning1")).to.beTrue()
done()
end,
failed = function(err)
error("HTTP request failed: " .. err)
done()
end
})
end
},
}
}
47 changes: 47 additions & 0 deletions tests/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3

import flask

app = flask.Flask(__name__)


@app.route("/", methods=["GET"])
def response_normal():
return b"Hello World!", 200


@app.route("/response_null_byte_in_body", methods=["GET"])
def response_null_byte_in_body():
return b"Hello World\0!", 200


@app.route("/response_multiple_cookies", methods=["GET"])
def response_multiple_cookies():
return (
b"Hello World!",
200,
{
"Set-Cookie": [
"CookieA=1",
"CookieB=2",
],
},
)


@app.route("/response_multiple_warning", methods=["GET"])
def response_multiple_warning():
return (
b"Hello World!",
200,
{
"Warning": [
"199 - Warning1",
"199 - Warning2",
],
},
)


if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0")

0 comments on commit 012199e

Please sign in to comment.