Skip to content

Commit

Permalink
Integration testing (#10)
Browse files Browse the repository at this point in the history
* Infra for integration tests
* Some integration tests
* `body` attribute in request object
  • Loading branch information
mishankov authored Jan 15, 2024
1 parent dcd9643 commit c009c58
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 8 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@ jobs:
nim-version: ${{ matrix.nim-version }}
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: nimble -d:ssl test
- name: Run unit tests
run: nimble unittests

- name: Run integation tests
# go-httpbin container does not run on windows runner for some reason. macos runner does not has docker by default
if: ${{ matrix.os == 'ubuntu-latest' }}
run: nimble inttests
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ htmldocs/

*.exe
nim.cfg

testresults/
testresults.html
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ All procedures above return `Response` object with fields:
- `url` - stores full url with query params
- `headers` - stores HTTP headers with `Authorization` for basic authorization
- `httpMethod` - HTTP method
- `body` - request body as a string

`Response` object has some helper procedures:
- `Response.json()` - returns response body as JSON
Expand Down
7 changes: 4 additions & 3 deletions src/yahttp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type
url*: string
headers*: seq[tuple[key: string, val: string]]
httpMethod*: Method
body*: string

Response* = object
## Type for HTTP response
Expand All @@ -41,14 +42,14 @@ type

proc toResp(response: httpclient.Response, requestUrl: string,
requestHeaders: seq[tuple[key: string, val: string]],
requestHttpMethod: Method): Response =
requestHttpMethod: Method, requestBody: string): Response =
## Converts httpclient.Response to yahttp.Response
return Response(
status: parseInt(response.status.strip()[0..2]),
headers: response.headers.table,
body: response.body,
request: Request(url: requestUrl, headers: requestHeaders,
httpMethod: requestHttpMethod)
httpMethod: requestHttpMethod, body: requestBody)
)

proc json*(response: Response): JsonNode =
Expand Down Expand Up @@ -123,7 +124,7 @@ proc request*(url: string, httpMethod: Method = Method.GET, headers: openArray[
client.close()

return response.toResp(requestUrl = innerUrl, requestHeaders = innerHeaders,
requestHttpMethod = httpMethod)
requestHttpMethod = httpMethod, requestBody = body)


# Gnerating procs for individual HTTP methods
Expand Down
1 change: 0 additions & 1 deletion tests/config.nims

This file was deleted.

2 changes: 2 additions & 0 deletions tests/int/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
switch("path", "$projectDir/../../src")
--d:ssl
40 changes: 40 additions & 0 deletions tests/int/test_yahttp.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
import json

include yahttp


const BASE_URL = "http://localhost:8080"

test "Test HTTP methods":
check put(BASE_URL & "/put").ok()
check post(BASE_URL & "/post").ok()
check patch(BASE_URL & "/patch").ok()
check delete(BASE_URL & "/delete").ok()

test "Test query params":
let jsonResp = get(BASE_URL & "/get", query={"param1": "value1", "param2": "value2"}).json()

check jsonResp["args"]["param1"][0].getStr() == "value1"
check jsonResp["args"]["param2"][0].getStr() == "value2"

test "Test headers":
let jsonResp = get(BASE_URL & "/headers", headers={"header1": "value1", "header2": "value2"}).json()

check jsonResp["headers"]["Header1"][0].getStr() == "value1"
check jsonResp["headers"]["Header2"][0].getStr() == "value2"

test "Test auth header":
let jsonResp = get(BASE_URL & "/headers", auth=("test login", "test_pass")).json()

check jsonResp["headers"]["Authorization"][0].getStr() == "Basic dGVzdCBsb2dpbjp0ZXN0X3Bhc3M="

test "Test body":
let jsonResp = put(BASE_URL & "/put", headers = {"Content-Type": "text/plain"}, body = "some body").json()

check jsonResp["data"].getStr() == "some body"

test "Test JSON body":
let jsonResp = put(BASE_URL & "/put", headers = {"Content-Type": "application/json"}, body = $ %*{"key": "value"}).json()

check jsonResp["json"]["key"].getStr() == "value"
2 changes: 2 additions & 0 deletions tests/unit/config.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
switch("path", "$projectDir/../../src")
--d:ssl
2 changes: 1 addition & 1 deletion tests/test_yahttp.nim → tests/unit/test_yahttp.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import unittest
import tables

include yahttp


test "Generate correct basic auth header":
check ("test login", "test_pass").basicAuthHeader() == "Basic dGVzdCBsb2dpbjp0ZXN0X3Bhc3M="

Expand Down
11 changes: 10 additions & 1 deletion yahttp.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@ task pretty, "Pretty":
exec "nimpretty src/yahttp.nim"

task examples, "Run examples":
exec "nim c --run examples/examples.nim "
exec "nim c --run examples/examples.nim"

task unittests, "Run unit tests":
exec "testament pattern \"tests/unit/*.nim\""

task inttests, "Run integation tests":
exec "docker run -d --name yahttp-httpbin -p 8080:8080 mccutchen/go-httpbin"
exec "testament pattern \"tests/int/*.nim\""
exec "docker stop yahttp-httpbin"
exec "docker remove yahttp-httpbin"

0 comments on commit c009c58

Please sign in to comment.