This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/api/server_for_test.go
2014-10-02 16:41:13 -08:00

43 lines
1,021 B
Go

package api
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/thermokarst/bactdb/datastore"
"github.com/thermokarst/bactdb/models"
)
func init() {
serveMux.Handle("/", http.StripPrefix("/api", Handler()))
}
var (
serveMux = http.NewServeMux()
httpClient = http.Client{Transport: (*muxTransport)(serveMux)}
apiClient = models.NewClient(&httpClient)
)
func setup() {
store = datastore.NewMockDatastore()
}
type muxTransport http.ServeMux
// RoundTrip is for testing API requests. It intercepts all requests during testing
// to serve up a local/internal response.
func (t *muxTransport) RoundTrip(req *http.Request) (*http.Response, error) {
rw := httptest.NewRecorder()
rw.Body = new(bytes.Buffer)
(*http.ServeMux)(t).ServeHTTP(rw, req)
return &http.Response{
StatusCode: rw.Code,
Status: http.StatusText(rw.Code),
Header: rw.HeaderMap,
Body: ioutil.NopCloser(rw.Body),
ContentLength: int64(rw.Body.Len()),
Request: req,
}, nil
}