1
0

client.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package testing
  15. import (
  16. "github.com/astaxie/beego/config"
  17. "github.com/astaxie/beego/httplib"
  18. )
  19. var port = ""
  20. var baseURL = "http://localhost:"
  21. // TestHTTPRequest beego test request client
  22. type TestHTTPRequest struct {
  23. httplib.BeegoHTTPRequest
  24. }
  25. func getPort() string {
  26. if port == "" {
  27. config, err := config.NewConfig("ini", "../conf/app.conf")
  28. if err != nil {
  29. return "8080"
  30. }
  31. port = config.String("httpport")
  32. return port
  33. }
  34. return port
  35. }
  36. // Get returns test client in GET method
  37. func Get(path string) *TestHTTPRequest {
  38. return &TestHTTPRequest{*httplib.Get(baseURL + getPort() + path)}
  39. }
  40. // Post returns test client in POST method
  41. func Post(path string) *TestHTTPRequest {
  42. return &TestHTTPRequest{*httplib.Post(baseURL + getPort() + path)}
  43. }
  44. // Put returns test client in PUT method
  45. func Put(path string) *TestHTTPRequest {
  46. return &TestHTTPRequest{*httplib.Put(baseURL + getPort() + path)}
  47. }
  48. // Delete returns test client in DELETE method
  49. func Delete(path string) *TestHTTPRequest {
  50. return &TestHTTPRequest{*httplib.Delete(baseURL + getPort() + path)}
  51. }
  52. // Head returns test client in HEAD method
  53. func Head(path string) *TestHTTPRequest {
  54. return &TestHTTPRequest{*httplib.Head(baseURL + getPort() + path)}
  55. }