另客网go项目公用的代码库

1234567891011121314151617181920212223242526272829303132333435363738
  1. package request
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. )
  8. // HttpRequestConfig http request config
  9. type HttpRequestConfig struct {
  10. Method string
  11. URL string
  12. Param io.Reader
  13. Header map[string]string
  14. Context context.Context
  15. }
  16. // AssertHttpRequest check http request
  17. func AssertHttpRequest(handler http.Handler, config HttpRequestConfig) *httptest.ResponseRecorder {
  18. r := httptest.NewRequest(config.Method, config.URL, config.Param)
  19. w := httptest.NewRecorder()
  20. if config.Header != nil {
  21. for k, v := range config.Header {
  22. r.Header.Set(k, v)
  23. }
  24. }
  25. if config.Context != nil {
  26. r.WithContext(config.Context)
  27. }
  28. handler.ServeHTTP(w, r)
  29. return w
  30. }