func sendRequest(method, path string, body interface{}, t *testing.T) *ResRecorder { if !IsInitialized() { e := gin.Default() apiHandler = e testConfig := envconfig.NewConfig() testConfig.M = map[string]interface{}{ "mongoHost": "localhost:27017", } Initialize(e, testConfig) db.Initialize(testConfig) httptest.NewServer(e) e.Use(func() gin.HandlerFunc { return func(c *gin.Context) { fmt.Println("###") fmt.Println("URL: -> " + c.Request.URL.String()) fmt.Println("Method: -> " + c.Request.Method) fmt.Println("Headers: ->") for k := range c.Request.Header { fmt.Println(c.Request.Header[k]) } fmt.Println("<-") if user, exists := c.Get("user"); exists { fmt.Println("User: -> ", user) } b, _ := ioutil.ReadAll(c.Request.Body) fmt.Println("Body: -> ", string(b)) fmt.Println("### -->>") c.Next() } }()) } var b string if body != nil { bodyStr, err := json.Marshal(body) if err != nil { panic(err) } b = fmt.Sprintf("%s", bodyStr) } req, err := http.NewRequest(method, "/v1"+path, strings.NewReader(b)) req.Header.Set("Authorization", "Bearer "+token) if err != nil { panic(err) } w := httptest.NewRecorder() apiHandler.ServeHTTP(w, req) return &ResRecorder{w, t} }
func main() { engine := gin.Default() c, err := envconfig.Load("neutrino") if err != nil { panic("Error loading config: " + err.Error()) } db.Initialize(c) api.Initialize(engine, c) engine.Run(c.GetOrDefault("port", ":5000").(string)) }