项目作者: tintinnabulate

项目描述 :
Google AppEngine context handlers for writing testable go webapps
高级语言: Go
项目地址: git://github.com/tintinnabulate/aecontext-handlers.git
创建时间: 2018-09-16T22:25:28Z
项目社区:https://github.com/tintinnabulate/aecontext-handlers

开源协议:MIT License

下载


aecontext-handlers

aecontext-handlers are a bunch of AppEngine context handlers that wrap the
standard net/http HandlerFunc, enabling you to easily switch out how the
AppEngine Context is created, depending on what environment (production / test)
the code is running in.

Example usage

Note that this is an AppEngine main application, hence there is no func main() {}.

main.go

  1. package main
  2. import (
  3. "github.com/gorilla/mux"
  4. "golang.org/x/net/context"
  5. "net/http"
  6. "github.com/tintinnabulate/aecontext-handlers/handlers"
  7. )
  8. // createHTTPRouter : creates our URL router. As an argument it takes in a
  9. // ToHandlerHOF, whose job is to make the conversion to a standard HandlerFunc()
  10. // format. This means we can change how the AppEngine Context gets created, by
  11. // passing in different ToHandlerHOF implementations. In this case, we use the one
  12. // we want for production (see `init()` function)
  13. func createHTTPRouter(f handlers.ToHandlerHOF) *mux.Router {
  14. appRouter := mux.NewRouter()
  15. appRouter.HandleFunc("/foo", f(fooHandler)).Methods("GET")
  16. return appRouter
  17. }
  18. // fooHandler : handle /foo
  19. func fooHandler(ctx context.Context, w http.ResponseWriter, req *http.Request) {
  20. }
  21. func init() {
  22. // create our URL router, passing in a HOF that converts to a standard HandlerFunc()
  23. router := createHTTPRouter(handlers.ToHTTPHandler)
  24. http.Handle("/", router)
  25. }

main_test.go

  1. package main
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. c "github.com/smartystreets/goconvey/convey"
  7. "github.com/tintinnabulate/aecontext-handlers/handlers"
  8. )
  9. // TestFooHandler : does just that
  10. func TestFooHandler(t *testing.T) {
  11. // Get our testing context and aetest instance
  12. ctx, inst := handlers.GetTestingContext()
  13. // Remember to close it at the end of every test!
  14. defer inst.Close()
  15. c.Convey("When user visits the foo page", t, func() {
  16. // create our router, using this testing context
  17. r := createHTTPRouter(handlers.ToHTTPHandlerConverter(ctx))
  18. record := httptest.NewRecorder()
  19. req, err := http.NewRequest("GET", "/foo", nil)
  20. c.So(err, c.ShouldBeNil)
  21. c.Convey("The status should equal http.StatusOK", func() {
  22. // use the router to serve the request, recording the response
  23. r.ServeHTTP(record, req)
  24. // Assert that the return code matches what we expect
  25. c.So(record.Code, c.ShouldEqual, http.StatusOK)
  26. })
  27. })
  28. }

Used by

Inspired by

This idea was taken, almost completely unmodified, from the excellent Compound Theory blog:

https://www.compoundtheory.com/testing-go-http-handlers-in-google-app-engine-with-mux-and-higher-order-functions

All credit goes to that :-)