func Test_InterfaceOf(t *testing.T) { iType := inject.InterfaceOf((*SpecialString)(nil)) expect(t, iType.Kind(), reflect.Interface) iType = inject.InterfaceOf((**SpecialString)(nil)) expect(t, iType.Kind(), reflect.Interface) // Expecting nil defer func() { rec := recover() refute(t, rec, nil) }() iType = inject.InterfaceOf((*testing.T)(nil)) }
func defaultReturnHandler() ReturnHandler { return func(ctx *Context, vals []reflect.Value) { rv := ctx.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) resp := rv.Interface().(http.ResponseWriter) var respVal reflect.Value if len(vals) > 1 && vals[0].Kind() == reflect.Int { resp.WriteHeader(int(vals[0].Int())) respVal = vals[1] } else if len(vals) > 0 { respVal = vals[0] if isError(respVal) { err := respVal.Interface().(error) if err != nil { ctx.internalServerError(ctx, err) } return } else if canDeref(respVal) { if respVal.IsNil() { return // Ignore nil error } } } if canDeref(respVal) { respVal = respVal.Elem() } if isByteSlice(respVal) { resp.Write(respVal.Bytes()) } else { resp.Write([]byte(respVal.String())) } } }
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. // While Martini is in development mode, Recovery will also output the panic as HTML. func Recovery() Handler { return func(c *Context, log *log.Logger) { defer func() { if err := recover(); err != nil { stack := stack(3) log.Printf("PANIC: %s\n%s", err, stack) // Lookup the current responsewriter val := c.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) res := val.Interface().(http.ResponseWriter) // respond with panic message while in development mode var body []byte if Env == DEV { res.Header().Set("Content-Type", "text/html") body = []byte(fmt.Sprintf(panicHtml, err, err, stack)) } res.WriteHeader(http.StatusInternalServerError) if nil != body { res.Write(body) } } }() c.Next() } }
func TestInjectImplementors(t *testing.T) { injector := inject.New() g := &Greeter{"Jeremy"} injector.Map(g) expect(t, injector.GetVal(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), true) }
func Test_InjectorSetParent(t *testing.T) { injector := inject.New() injector.MapTo("another dep", (*SpecialString)(nil)) injector2 := inject.New() injector2.SetParent(injector) expect(t, injector2.GetVal(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), true) }
func defaultReturnHandler() ReturnHandler { return func(ctx *Context, vals []reflect.Value) { rv := ctx.GetVal(inject.InterfaceOf((*http.ResponseWriter)(nil))) res := rv.Interface().(http.ResponseWriter) var respVal reflect.Value if len(vals) > 1 && vals[0].Kind() == reflect.Int { res.WriteHeader(int(vals[0].Int())) respVal = vals[1] } else if len(vals) > 0 { respVal = vals[0] } if canDeref(respVal) { respVal = respVal.Elem() } if isByteSlice(respVal) { res.Write(respVal.Bytes()) } else { res.Write([]byte(respVal.String())) } } }