diff --git a/context/context.go b/context/context.go index d2274aa..b3b1f4e 100644 --- a/context/context.go +++ b/context/context.go @@ -2,6 +2,7 @@ package context import ( "context" + "fmt" "net/http" ) @@ -11,5 +12,10 @@ type Store interface { func Server(store Store) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + data, err := store.Fetch(r.Context()) + if err != nil { + return + } + fmt.Fprint(w, data) } } diff --git a/context/context_test.go b/context/context_test.go index 5dfe90e..da89fcf 100644 --- a/context/context_test.go +++ b/context/context_test.go @@ -2,6 +2,7 @@ package context import ( "context" + "errors" "log" "net/http" "net/http/httptest" @@ -40,6 +41,24 @@ func (s *SpyStore) Fetch(ctx context.Context) (string, error) { } } +type SpyResponseWriter struct { + written bool +} + +func (s *SpyResponseWriter) Header() http.Header { + s.written = true + return nil +} + +func (s *SpyResponseWriter) Write([]byte) (int, error) { + s.written = true + return 0, errors.New("not implemented") +} + +func (s *SpyResponseWriter) WriteHeader(statusCode int) { + s.written = true +} + func TestServer(t *testing.T) { t.Run("returns data from store", func(t *testing.T) { data := "hello, world" @@ -67,8 +86,12 @@ func TestServer(t *testing.T) { time.AfterFunc(5*time.Millisecond, cancel) request = request.WithContext(cancellingCtx) - response := httptest.NewRecorder() + response := &SpyResponseWriter{} svr.ServeHTTP(response, request) + + if response.written { + t.Error("a response should not have been written") + } }) }