Ejemplo n.º 1
0
func TestSettingsContextMiddleware(t *testing.T) {
	Convey("Given a server with settings context middleware", t, func() {
		// Reset settings on each test
		configuration.ResetSettings()

		// Set the negroni server with our middleware
		response := httptest.NewRecorder()
		n := negroni.New()
		n.Use(NewSettingsContext())
		req, _ := http.NewRequest("GET", "http://localhost:3000/", nil)

		// Check no context before running the middleware
		Convey("The starting context should be empty", func() {
			So(len(context.GetAll(req)), ShouldEqual, 0)
		})

		Convey("When making a request", func() {

			n.ServeHTTP(response, req)
			requestContextAfter := context.GetAll(req)
			settings := viper.AllSettings()

			Convey("The context should be the same as the app settings", func() {
				So(len(requestContextAfter), ShouldEqual, len(requestContextAfter))
				for k := range settings {
					So(requestContextAfter[k], ShouldEqual, settings[k])
				}
			})
		})

		Reset(func() {
		})
	})
}
Ejemplo n.º 2
0
// GetContextUser returns the User for the given request context or nil.
func GetContextUser(r *http.Request) *User {
	log.Debug("context contents are...", context.GetAll(r))
	if user, ok := context.GetOk(r, contextUser); ok {
		return user.(*User)
	}
	log.Debug("in GetContextUser...no user!")
	return nil
}
Ejemplo n.º 3
0
func DoTemplate(template string, rw http.ResponseWriter, req *http.Request) {
	TemplatesLock.Lock()
	defer TemplatesLock.Unlock()
	t, ok := Templates[template]
	if ok {
		err := t.Execute(rw, context.GetAll(req))
		if err != nil {
			log.Printf("DoTemplate ERR: %s", err.Error())
		}
	} else {
		log.Fatalf("DoTemplate called with missing template: %s", template)
	}
}
Ejemplo n.º 4
0
// ContextFields will take a request and convert a context map to logrus Fields.
func ContextFields(r *http.Request) map[string]interface{} {
	fields := map[string]interface{}{}
	for k, v := range context.GetAll(r) {
		strK := fmt.Sprintf("%+v", k)
		// gorilla.mux adds the route to context.
		// we want to remove it for now
		if strK == "1" {
			continue
		}
		// gorilla puts mux vars here, we want to give a better label
		if strK == "0" {
			strK = "muxvars"
		}
		fields[strK] = fmt.Sprintf("%#+v", v)
	}
	fields["path"] = r.URL.Path
	fields["rawquery"] = r.URL.RawQuery

	return fields
}
Ejemplo n.º 5
0
// ContextFields will take a request and convert a context map to logrus Fields.
func ContextFields(r *http.Request) map[string]interface{} {
	fields := map[string]interface{}{}
	for k, v := range context.GetAll(r) {
		strK := fmt.Sprintf("%+v", k)
		typeK := fmt.Sprintf("%T-%+v", k, k)
		// gorilla.mux adds the route to context.
		// we want to remove it for now
		if typeK == "mux.contextKey-1" || typeK == "mux.contextKey-0" {
			continue
		}
		// web.varsKey for _all_ mux variables (gorilla or httprouter)
		if typeK == "web.contextKey-2" {
			strK = "muxvars"
		}
		fields[strK] = fmt.Sprintf("%#+v", v)
	}
	fields["path"] = r.URL.Path
	fields["rawquery"] = r.URL.RawQuery

	return fields
}
Ejemplo n.º 6
0
func copyRequestVars(old, new *http.Request) {
	kv := context.GetAll(old)
	for k, v := range kv {
		context.Set(new, k, v)
	}
}