forked from quii/learn-go-with-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
context - wip - refactor to pass context through call stack
- Loading branch information
Showing
5 changed files
with
308 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// Store fetches data | ||
type Store interface { | ||
Fetch(ctx context.Context) (string, error) | ||
} | ||
|
||
// Server returns a handler for calling Store | ||
func Server(store Store) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
data, err := store.Fetch(r.Context()) | ||
|
||
if err != nil { | ||
return // todo: log error however you like | ||
} | ||
|
||
fmt.Fprint(w, data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
data := "hello, world" | ||
|
||
t.Run("returns data from store", func(t *testing.T) { | ||
store := &SpyStore{response: data, t: t} | ||
svr := Server(store) | ||
|
||
request := httptest.NewRequest(http.MethodGet, "/", nil) | ||
response := httptest.NewRecorder() | ||
|
||
svr.ServeHTTP(response, request) | ||
|
||
if response.Body.String() != data { | ||
t.Errorf(`got "%s", want "%s"`, response.Body.String(), data) | ||
} | ||
|
||
if store.ctx != request.Context() { | ||
t.Errorf("store was not passed through a context %v", store.ctx) | ||
} | ||
}) | ||
|
||
t.Run("tells store to cancel work if request is cancelled", func(t *testing.T) { | ||
store := &SpyStore{response: data, t: t} | ||
svr := Server(store) | ||
|
||
request := httptest.NewRequest(http.MethodGet, "/", nil) | ||
|
||
cancellingCtx, cancel := context.WithCancel(request.Context()) | ||
time.AfterFunc(5*time.Millisecond, cancel) | ||
request = request.WithContext(cancellingCtx) | ||
|
||
response := &SpyResponseWriter{} | ||
|
||
svr.ServeHTTP(response, request) | ||
|
||
if response.written { | ||
t.Error("a response should not have been written") | ||
} | ||
}) | ||
} |
Oops, something went wrong.