Serve multiple handlers with httptest to mock multiple requests - go

I've googled all over for this but can't find anything.
I have a struct that takes in a http.Client and it sends several GET requests. In my tests I want to mock the responses so it's not sending real requests.
Currently I've figured out how to only serve 1 request, like this:
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
file, err := os.Open("./testdata/1.html")
if err != nil {
t.Error(err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
t.Error(err)
}
w.Write(bytes)
}))
ts.Client() // Now I can inject this client into my struct.
So once that response is mocked out and the http client is performs a new request, my tests are sending out real requests after that.
How do I allow for several handlers so I can mock several responses upon calling http.Client.Get(...)?

Since the original question uses httptest.NewServer - you can register a ServeMux on the httptest.Server function, and then you can add several routes to that mux:
mux := http.NewServeMux()
mux.HandleFunc("/someroute/", func(res http.ResponseWriter, req *http.Request) {
...do some stuff...
})
mux.HandleFunc("/someotherroute/", func(res http.ResponseWriter, req *http.Request) {
...do other stuff...
})
ts := httptest.NewServer(mux)
defer ts.Close()

ServeMux.Handle can be used to setup a server to handle multiple requests like in this example.
package main
import (
"log"
"net/http"
)
const addr = "localhost:12345"
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/hello", HandleHello)
// other handlers can be assigned to separate paths
log.Printf("Now listening on %s...\n", addr)
server := http.Server{Handler: mux, Addr: addr}
log.Fatal(server.ListenAndServe())
}
func HandleHello(w http.ResponseWriter, r *http.Request) {
log.Printf("Hello!")
}
But to be honest you probably just want to abstract the http.Client behind an interface that you've created, and then stub that with a test implementation that returns exactly what you want. By doing this you avoid the overhead of http communication in your tests.

Related

Go rest handler views every request as GET

So basically I am trying to build this simple REST API, and I have this createCartHandler that every time I call it using postman it looks like the request method is GET. So no matter how I do the request, r.Method will be GET. Do you know what could cause this?
Also, if I do the request using something like cURL (curl -XPOST'http://localhost:8080/v1/create') it looks like it doesn't even view the request, like it is pointless.
type Service struct {
cache *lru.Cache
}
func (s *Service) createCartHandler() http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method)
....
})
}
func main() {
cache, err := lru.New(1024)
if err != nil {
log.Panicf("Faild to create cache: %v", err)
}
svc := &Service{
cache: cache,
}
...
mux := http.NewServeMux()
mux.Handle("/v1/create/", svc.createCartHandler())
address := hostport
...
log.Print("Listening on ", hostport)
// When running on docker mac, can't listen only on localhost
panic(http.ListenAndServe(address, mux))
}

Shutdown web server after the request is handled - Go

I have a function that when run will give a user a URL to use to start an OAuth flow for a token exchange with my client.
I need to run a local HTTP server to accept the callback for the user. But I'm not sure how to then shutdown the HTTP server and close the function and move on once the flow is done.
func OAuth(request *OAuthRequest) {
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"response": "unable to handle request"}`))
// Do something
// Finish up and shutdown HTTP server
})
err := http.ListenAndServe(net.JoinHostPort("","8080"), nil)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Login at: www.example.com/123abc")
// Wait for user to finish flow
}
User calls .OAuth()
Local server is started and exposes /callback
User is given URL to use in browser
Remote server sends callback to localhost/callback
Local HTTP server handles response
Job done, shutdown local HTTP server
.OAuth() is complete
You need to create the HTTP server via http.Server if you want to be able to shut it down:
package main
import (
"context"
"fmt"
"net/http"
)
func main() {
mux := http.NewServeMux()
srv := http.Server{
Addr: "localhost:8080",
Handler: mux,
}
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"response": "unable to handle request"}`))
go srv.Shutdown(context.Background())
})
fmt.Println("Login at: www.example.com/123abc")
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
panic(err)
}
}

Middleware using Alice and HttpRouter

I can't seem to work out how to use middleware and Http Router properly together.
My code is:
type appContext struct {
db *mgo.Database
}
func main(){
c := appContext{session.DB("db-name")}
commonHandlers := alice.New(context.ClearHandler, basicAuthHandler)
router := NewRouter()
router.Post("/", commonHandlers.ThenFunc(c.final))
http.ListenAndServe(":5000", router)
}
The final middleware is:
func (c *appContext) final(w http.ResponseWriter, r *http.Request) {
log.Println("Executing finalHandler")
w.Write([]byte("TESTING"))
}
but I want my basicAuthHandler to be part of the commonHandlers. It also needs the context so that I can query the db.
I have tried this:
func (c *appContext) basicAuthHandler(w http.ResponseWriter, r *http.Request) {
var app App
err := c.db.C("apps").Find(bson.M{"id":"abcde"}).One(&app)
if err != nil {
panic(err)
}
//do something with the app
}
but I get the error undefined: basicAuthHandler. I understand why I'm getting the error but I don't know how to avoid it. How can I provide the context to the basicAuthHandler and still use it in the commonHandlers list for Alice?
Your middleware needs to have the signature
func(http.Handler) http.Handler
This way your middleware is wrapping handlers, not just providing a final handler. You need to accept an http.Handler, do whatever processing needs to be done, and call ServeHTTP on the next handler in the chain. Your basicAuthHandler example could look like this:
func (c *appContext) basicAuthHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var app App
err := c.db.C("apps").Find(bson.M{"id": "abcde"}).One(&app)
if err != nil {
panic(err)
}
h.ServeHTTP(w, r)
})
}
(though you don't want to panic in your app, and should provide a better error response)

Golang net/http and Gorilla: run code before handler

Is it possible using the net/http package and/or any of the gorilla libraries to make some code execute on EVERY URL before going to the handler? For example, to check if a connection is coming from a black listed IP address?
Create a handler that invokes another handler after checking the IP address:
type checker struct {
h http.Handler
}
func (c checker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if blackListed(r.RemoteAddr) {
http.Error(w, "not authorized", http.StatusForbidden)
return
}
c.h.ServeHTTP(w, r)
}
Pass this handler to ListenAndServe instead of your original handler. For example, if you had:
err := http.ListenAndServe(addr, mux)
change the code to
err := http.ListenAndServe(addr, checker{mux})
This also applies to all the variations of ListenAndServe. It works with http.ServeMux, Gorilla mux and other routers.
If you want to use the default muxer, which i find is common, you can create middleware like so:
func mustBeLoggedIn(handler func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Am i logged in?
if ...not logged in... {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Pass through to the original handler.
handler(w, r)
}
}
Use it like so:
http.HandleFunc("/some/priveliged/action", mustBeLoggedIn(myVanillaHandler))
http.ListenAndServe(":80", nil)

Best practice with sessions (gorilla/sessions)

Before starting using sessions in golang I need answers to some questions
session example
import "github.com/gorilla/sessions"
var store = sessions.NewCookieStore([]byte("33446a9dcf9ea060a0a6532b166da32f304af0de"))
func Handler(w http.ResponseWriter, r *http.Request){
session, _ := store.Get(r, "session-name")
session.Values["foo"] = "bar"
session.Values[42] = 43
session.Save(r, w)
fmt.Fprint(w, "Hello world :)")
}
func main(){
store.Options = &sessions.Options{
Domain: "localhost",
Path: "/",
MaxAge: 60 * 15,
Secure: false,
HttpOnly: true,
}
}
Q1:
Is it possible to add multiple sessions on the same domain with different names?
session1, _ := store.Get(r, "session-name-1")
session2, _ := store.Get(r, "session-name-2")
When do you need multiple sessions on the same domain?
Q2:
What is the best practice to get the variables from the session?
my_session_var = session.Values["foo"]
Q3:
How to check if the session is saved correctly? If you access the same map to both set and get variables?
update
package main
import (
"github.com/gorilla/sessions"
)
var (
store = sessions.NewCookieStore([]byte("33446a9dcf9ea060a0a6532b166da32f304af0de"))
)
type handler func(w http.ResponseWriter, r *http.Request, s *sessions.Session)
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request){
session, _ := store.Get(r, "session-name")
h(w, r, session)
}
func Handler_404(w http.ResponseWriter, r *http.Request, s *sessions.Session){
fmt.Fprint(w, "Oops, something went wrong!")
}
error
# command-line-arguments
.\mux.go:101: cannot convert Handler_404 (type func(http.ResponseWriter, *http.Request, *sessions.Session)) to type http.HandlerFunc
The article "BASIC EXTENSION OF GO’S HTTP HANDLERS" (Simon Whitehead) shows an example of where and when to define session.
Instead of doing it in the Handler itself, and having to duplicate a lot of code when you define other Handlers.
With a named type, you can define the Handler you need:
type handler func(w http.ResponseWriter, r *http.Request, db *mgo.Database)
(in your case, it would be a gorilla sessions instead of a mgo session or database)
The init() function can take care of the session creation (here mgo session, but the idea is the same for other framework sessions)
func init() {
session, err = mgo.Dial("localhost")
if err != nil {
log.Println(err)
}
}
And you can make sure this function type ('handler') does respect the ServeHTTP() function, taking care of:
the session management (clone/close)
calling your actual handler (which can have more parameters than just w and r)
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s := session.Clone()
defer s.Close()
h(w, r, s.DB("example"))
}
Then you can define your actual Handler (again, with more than w and r):
func myHandler(w http.ResponseWriter, r *http.Request, db *mgo.Database) {
var users []user
db.C("users").Find(nil).All(&users)
for _, user := range users {
fmt.Fprintf(w, "%s is %d years old", user.Name, user.Age)
}
}
And you can use that handler in your server:
func main() {
mux := http.NewServeMux()
mux.Handle("/", handler(myHandler))
http.ListenAndServe(":8080", mux)
}
The idea is to limit the "plumbing" in main() to a minimum, while having an Handler with more parameters (including your session).
That allows you to use different Handlers with very little plumbing, keeping main() only for the declaration of the different path (and not for the initialization of session and handlers)
Update 2019: in another related context, see also "How to handle sessions".

Resources