Skip to content

Instantly share code, notes, and snippets.

@chrisoldwood
Created October 12, 2022 16:25
Show Gist options
  • Save chrisoldwood/9ea9243e352e56bb36829c15265c2223 to your computer and use it in GitHub Desktop.
Save chrisoldwood/9ea9243e352e56bb36829c15265c2223 to your computer and use it in GitHub Desktop.
Example unit tests for verifying outbound HTTP request parameters in a pure functional language
private anyHostname = builders.randomHostname(10)
private anyToken = builders.randomToken(10)
private anyResourcePath = builders.randomPathName(8) ~ "/" ~ builders.randomPathName(8)
private emptyResponse = "[]"
Test2(Fixture, "Fetching a resource passes the access token in the PRIVATE-TOKEN header", () => {
token = builders.randomToken(12)
getMethod = (url, headers) => {
assert.that(headers, isTableWithKey("PRIVATE-TOKEN"), "The access token header is missing")
assert.that(get(headers, "PRIVATE-TOKEN", ""), isEqualTo(token), "The access token was incorrect")
in emptyResponse
}
netClient = netclientFake.create(getMethod)
gitlabClient = gitlab._acquireClient(anyHostname, token, netClient)
model = gitlabClient.getResource(anyResourcePath)
in assert.pass()
})
Test2(Fixture, "Fetching a resource uses the v4 API", () => {
hostname = "hostname"
getMethod = (url, headers) => {
assert.that(url, isStringStarting("https://"~hostname~"/api/v4"), "The base URL is incorrect")
in emptyResponse
}
netClient = netclientFake.create(getMethod)
gitlabClient = gitlab._acquireClient(hostname, anyToken, netClient)
model = gitlabClient.getResource(anyResourcePath)
in assert.pass()
})
@chrisoldwood
Copy link
Author

in this case, you could imagine a pure function called getApiRequestParams or something...

Yeah, I can see how to break the problem down into smaller pure functions but then it feels like the overall design is being warped just to allow testing. Of course Design for Testability generally has this affect but its nicer if you don't have to warp things too much.

Thanks for the feedback, it's good food for thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment