Golang gin http test

jeonghyeon·2022년 7월 26일
0

gin handler를 테스트하는 예제 코드

package handler_test

import (
	"ginhttptest/handler"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/assert"
)

func TestHandler(t *testing.T) {
	rPath := "/user"
	router := gin.Default()
	router.GET(rPath, handler.Handler)
	req, _ := http.NewRequest("GET", rPath, nil)
	
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)

	assert.Equal(t, http.StatusOK, w.Code, "response status should be 200")
}

0개의 댓글