HTTP testing
In Chapter 11, Writing Networked Services, we saw that Go offers first-class APIs to build client and server programs using HTTP. The net/http/httptest
sub-package, part of the Go standard library, facilitates the testing automation of both HTTP server and client code, as discussed in this section.
To explore this space, we will implement a simple API service that exposes the vector operations (covered in earlier sections) as HTTP endpoints. For instance, the following source snippet partially shows the methods that make up the server (for a complete listing, see https://github.com/vladimirvivien/learning-go/ch12/service/serv.go):
package main import ( "encoding/json" "fmt" "net/http" "github.com/vladimirvivien/learning-go/ch12/vector" ) func add(resp http.ResponseWriter, req *http.Request) { var params []vector.SimpleVector if err := json.NewDecoder(req.Body).Decode(¶ms); err != nil { ...