Go语言测试与性能调优之http测试

一、通过使用假的Request/Response
示例1:

// 没有错误
const noErrorStr  = "no error"
func noError(writer http.ResponseWriter, request *http.Request) error {
   fmt.Fprintln(writer, noErrorStr)
   return nil
}

type testingUserError string

func (error testingUserError) Error() string {
   return string(error)
}

func (error testingUserError) Message() string {
   return  error.Error()
}

// userError
const userErrorStr  = "user error"
func userErr(writer http.ResponseWriter, request *http.Request) error {
   return testingUserError(userErrorStr)
}

// notFoundError
func notFoundErr(writer http.ResponseWriter, request *http.Request) error {
   return os.ErrNotExist
}

// forbiddenError
func forbiddenErr(writer http.ResponseWriter, request *http.Request) error {
   return os.ErrPermission
}

// unknownError
const unknownErrorStr = "unknown error"
func unknownErr(writer http.ResponseWriter, request *http.Request) error {
   return errors.New(unknownErrorStr)
}

// panicError
const panicErrorStr = "panic error"
func panicErr(writer http.ResponseWriter, request *http.Request) error {
   panic(panicErrorStr)
}

var testDatas = []struct{
   handler appHandler
   code int
   message string
} {
   {noError, http.StatusOK, noErrorStr},
   {userErr, http.StatusBadRequest, userErrorStr},
   {notFoundErr, http.StatusNotFound, http.StatusText(http.StatusNotFound)},
   {forbiddenErr, http.StatusForbidden, http.StatusText(http.StatusForbidden)},
   {unknownErr, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)},
   {panicErr, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)},
}

func verifyResponseData(response *http.Response, expectCode int, expectMessage string, t *testing.T)  {
   actualCode := response.StatusCode
   actualMessageBytes, _ := ioutil.ReadAll(response.Body)
   actualMessage := strings.Trim(string(actualMessageBytes), "\n")

   if actualCode != expectCode || actualMessage != expectMessage {
      t.Errorf("Assertion Error: Expected(%d, %s), Actual(%d, %s)", expectCode, expectMessage, actualCode,
         actualMessage)
   }
}

func TestWrapper(t *testing.T)  {
   for _, testData := range testDatas {
      f := errWrapper(testData.handler)

      response := httptest.NewRecorder()
      request := httptest.NewRequest(http.MethodGet, "http://www.baidu.com", nil)

      f(response, request)

      verifyResponseData(response.Result(), testData.code, testData.message, t)
   }
}

二、通过起服务器
示例2:

// 没有错误
const noErrorStr  = "no error"
func noError(writer http.ResponseWriter, request *http.Request) error {
   fmt.Fprintln(writer, noErrorStr)
   return nil
}

type testingUserError string

func (error testingUserError) Error() string {
   return string(error)
}

func (error testingUserError) Message() string {
   return  error.Error()
}

// userError
const userErrorStr  = "user error"
func userErr(writer http.ResponseWriter, request *http.Request) error {
   return testingUserError(userErrorStr)
}

// notFoundError
func notFoundErr(writer http.ResponseWriter, request *http.Request) error {
   return os.ErrNotExist
}

// forbiddenError
func forbiddenErr(writer http.ResponseWriter, request *http.Request) error {
   return os.ErrPermission
}

// unknownError
const unknownErrorStr = "unknown error"
func unknownErr(writer http.ResponseWriter, request *http.Request) error {
   return errors.New(unknownErrorStr)
}

// panicError
const panicErrorStr = "panic error"
func panicErr(writer http.ResponseWriter, request *http.Request) error {
   panic(panicErrorStr)
}

var testDatas = []struct{
   handler appHandler
   code int
   message string
} {
   {noError, http.StatusOK, noErrorStr},
   {userErr, http.StatusBadRequest, userErrorStr},
   {notFoundErr, http.StatusNotFound, http.StatusText(http.StatusNotFound)},
   {forbiddenErr, http.StatusForbidden, http.StatusText(http.StatusForbidden)},
   {unknownErr, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)},
   {panicErr, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)},
}

func verifyResponseData(response *http.Response, expectCode int, expectMessage string, t *testing.T)  {
   actualCode := response.StatusCode
   actualMessageBytes, _ := ioutil.ReadAll(response.Body)
   actualMessage := strings.Trim(string(actualMessageBytes), "\n")

   if actualCode != expectCode || actualMessage != expectMessage {
      t.Errorf("Assertion Error: Expected(%d, %s), Actual(%d, %s)", expectCode, expectMessage, actualCode,
         actualMessage)
   }
}

func TestWrapperInServer(t *testing.T)  {
   for _, testData := range testDatas {
      f := errWrapper(testData.handler)

      server := httptest.NewServer(http.HandlerFunc(f))

      response, _ := http.Get(server.URL)

      verifyResponseData(response, testData.code, testData.message, t)
   }
}

你可能感兴趣的:(Go)