I'm writing an http client for a service and for testing I want to use a net/http/httptest server instead of calling out to the remote API. I can easily do this if I make the baseUrl a global variable which gets set to the url of my test server. However, this makes the production code more fragile because baseUrl can also be changed during runtime. My preference would be to make baseUrl a const for production code but still be able to change.
package main
const baseUrl = "http://google.com"
// in main_test.go
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
...
}
const baseUrl = ts.URL
// above line throws const baseUrl already defined error
Aucun commentaire:
Enregistrer un commentaire