-
Notifications
You must be signed in to change notification settings - Fork 0
HTTP Requests
The goal here is to give an example of a simple HTTP request.
First make a package in GOPATH:src
called requestTut/
, with two files requestTut.go
and requestTut_test.go
.
Next, let's head on over to requestTut_test.go
to write our test function. We follow the AAA unit test format and first arrange our test space:
package requestTut
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMyGETRequest(t *testing.T) {
// Arrange
endpoint := ""
// This recorder will capture responses from our test handler
rr := httptest.NewRecorder()
// This is our test handler.
// It is simple and just responds with everything being OK
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
}
Next we will Act. Since we are testing an HTTP Request, our Act stage should involve making the HTTP request and sending it off:
func TestMyGETRequest(t *testing.T) {
...
// Act
// We create our get request here
req, err := MyGETRequest(endpoint)
if err != nil {
t.Errorf(err.Error())
}
// Here we bind the response recorder and request
// to the hander. This essentially means req
// will be sent to the handler, and rr will
// record what the handler send back
handler.ServeHTTP(rr, req)
}
Finally, we make our Assertions:
func TestMyGETRequest(t *testing.T) {
...
// Assert
// Our handler should respond with a StatusOK response
// Here we make that assertion
if status := rr.Code; status != http.StatusOK {
t.Errorf("Wanted %d, got %d", http.StatusOK, status)
}
}
Now that we're done with the test part, let's make the actual MyGETRequest
function to pass it in requestTut.go
:
package requestTut
import (
"fmt"
"net/http"
)
// This builds a new Get Request
// It will use the GET HTTP Method and the target
// is the endpoint parameter
func MyGETRequest(endpoint string) (*http.Request, error) {
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
panic(err)
}
return req, nil
}
Running the test in requestTut_test.go
should now pass:
Running tool: /usr/bin/go test -timeout 30s requestTut -run ^(TestMyGETRequest)$
ok requestTut 0.009s
Success: Tests passed.
To see how this request would be used in a main
function, we can create one in requestTut.go
:
package main // Change package to be a main package
...
func main() {
// We'll use this website as an endpoint
endpoint := "http://httpstat.us/200"
// Here we use our function to build a GET request
req, err := MyGETRequest(endpoint)
if err != nil {
panic(err)
}
// This creates a client that will launch our request
cli := new(http.Client)
// "cli.Do" will launch our request and immediately
// return whatever respone is sent back from the target
resp, err := cli.Do(req)
if err != nil {
panic(err)
}
// The response has a status code, which describes
// the condition of the handling that happened
// at the target. You may be familiar with 404
// errors, also known as the StatusNotFound Code.
// In this case, the status code should be a 200,
// which is a StatusOK code, meaning everything
// is fine.
fmt.Printf("Response code: %d\n", resp.StatusCode)
}
Now running go run requestTut.go
will give us this output:
Response code: 200