Exemple #1
0
func sendRequest(method, path string, body interface{}, t *testing.T) *ResRecorder {
	if !IsInitialized() {
		e := gin.Default()
		apiHandler = e
		testConfig := nconfig.Load()
		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}
}
Exemple #2
0
func main() {
	engine := gin.Default()

	c := nconfig.Load()

	db.Initialize(c)
	api.Initialize(engine, c)

	engine.Run(c.GetString(nconfig.KEY_CORE_PORT))
}