HTTP Server testing in Go
- Fibonacci function: In the sequence of x,y,z numbers number z returns the sum of x+y. Our webserver returns the fibonacci sequence (or error) for any given number.
-
Create an endpoint (HandleFunc, "/fib", server by handleFib function )
-
Start a server 8080 (ListenAndServe)
-
Write a "Response-Request" function. We are getting a
num
parameter from request.(FormValue function) -
Need to convert into
int
since function takesint
(FormValue) -
Atoi function returns
int
or error (StatusBadRequest) -
If convertion is good, returns a line
-
Converting again: io.WriteString (int-->line)
-
Define Fib function
-
Create cache and initialize it with init function.
-
Testing function Handle Fib (using HTTPPIE, curl etc.)
-
Not testing a server, just a HandleFib function
-
Http test has recorder
-
Write test cases. We use
bytes
since response comes in bytes. -
Handler returns --serve http-->fake request
-
Add cycle
-
http->recorder
-
Fake request (GET, sprintf)
-
Call ServeHTTP -assert -body ->returns bytes -code status