import ( "net/http" "github.com/onsi/gomega/ghttp" ) // Start a new GHTTP server on localhost:8080 server := ghttp.NewServer() // Set a handler for GET requests server.RouteToHandler("GET", "/hello", ghttp.RespondWith(http.StatusOK, "Hello, World!")) // Make a GET request to the /hello endpoint on the GHTTP server resp, _ := http.Get(server.URL() + "/hello")
// Set up a new GHTTP server server := ghttp.NewServer() // Set up a sequence of handlers for requests to the /hello endpoint server.AppendHandlers( ghttp.CombineHandlers( ghttp.VerifyRequest("GET", "/hello"), ghttp.RespondWith(http.StatusOK, "Hello"), ), ghttp.CombineHandlers( ghttp.VerifyRequest("POST", "/hello"), ghttp.RespondWith(http.StatusCreated, "New resource created!"), ), ) // Make a GET request to the /hello endpoint resp, _ := http.Get(server.URL() + "/hello") // Make a POST request to the /hello endpoint req, _ := http.NewRequest("POST", server.URL()+"/hello", nil) resp, _ := http.DefaultClient.Do(req)This code creates a new GHTTP server and uses `Server.AppendHandlers()` to set up a sequence of handlers for requests to the /hello endpoint. The first handler responds to GET requests with "Hello", and the second handler responds to POST requests with "New resource created!". Finally, it makes a GET request and a POST request to the /hello endpoint and stores the responses.