conn_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package oss
  2. import (
  3. "net/http"
  4. . "gopkg.in/check.v1"
  5. )
  6. type OssConnSuite struct{}
  7. var _ = Suite(&OssConnSuite{})
  8. func (s *OssConnSuite) TestURLMarker(c *C) {
  9. um := urlMaker{}
  10. um.Init("docs.github.com", true, false)
  11. c.Assert(um.Type, Equals, urlTypeCname)
  12. c.Assert(um.Scheme, Equals, "http")
  13. c.Assert(um.NetLoc, Equals, "docs.github.com")
  14. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://docs.github.com/object?params")
  15. c.Assert(um.getURL("bucket", "object", "").String(), Equals, "http://docs.github.com/object")
  16. c.Assert(um.getURL("", "object", "").String(), Equals, "http://docs.github.com/object")
  17. c.Assert(um.getResource("bucket", "object", "subres"), Equals, "/bucket/object?subres")
  18. c.Assert(um.getResource("bucket", "object", ""), Equals, "/bucket/object")
  19. c.Assert(um.getResource("", "object", ""), Equals, "/")
  20. um.Init("https://docs.github.com", true, false)
  21. c.Assert(um.Type, Equals, urlTypeCname)
  22. c.Assert(um.Scheme, Equals, "https")
  23. c.Assert(um.NetLoc, Equals, "docs.github.com")
  24. um.Init("http://docs.github.com", true, false)
  25. c.Assert(um.Type, Equals, urlTypeCname)
  26. c.Assert(um.Scheme, Equals, "http")
  27. c.Assert(um.NetLoc, Equals, "docs.github.com")
  28. um.Init("docs.github.com:8080", false, true)
  29. c.Assert(um.Type, Equals, urlTypeAliyun)
  30. c.Assert(um.Scheme, Equals, "http")
  31. c.Assert(um.NetLoc, Equals, "docs.github.com:8080")
  32. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://bucket.docs.github.com:8080/object?params")
  33. c.Assert(um.getURL("bucket", "object", "").String(), Equals, "http://bucket.docs.github.com:8080/object")
  34. c.Assert(um.getURL("", "object", "").String(), Equals, "http://docs.github.com:8080/")
  35. c.Assert(um.getResource("bucket", "object", "subres"), Equals, "/bucket/object?subres")
  36. c.Assert(um.getResource("bucket", "object", ""), Equals, "/bucket/object")
  37. c.Assert(um.getResource("", "object", ""), Equals, "/")
  38. um.Init("https://docs.github.com:8080", false, true)
  39. c.Assert(um.Type, Equals, urlTypeAliyun)
  40. c.Assert(um.Scheme, Equals, "https")
  41. c.Assert(um.NetLoc, Equals, "docs.github.com:8080")
  42. um.Init("127.0.0.1", false, true)
  43. c.Assert(um.Type, Equals, urlTypeIP)
  44. c.Assert(um.Scheme, Equals, "http")
  45. c.Assert(um.NetLoc, Equals, "127.0.0.1")
  46. um.Init("http://127.0.0.1", false, false)
  47. c.Assert(um.Type, Equals, urlTypeIP)
  48. c.Assert(um.Scheme, Equals, "http")
  49. c.Assert(um.NetLoc, Equals, "127.0.0.1")
  50. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://127.0.0.1/bucket/object?params")
  51. c.Assert(um.getURL("", "object", "params").String(), Equals, "http://127.0.0.1/?params")
  52. um.Init("https://127.0.0.1:8080", false, false)
  53. c.Assert(um.Type, Equals, urlTypeIP)
  54. c.Assert(um.Scheme, Equals, "https")
  55. c.Assert(um.NetLoc, Equals, "127.0.0.1:8080")
  56. }
  57. func (s *OssConnSuite) TestAuth(c *C) {
  58. endpoint := "https://github.com/"
  59. cfg := getDefaultOssConfig()
  60. um := urlMaker{}
  61. um.Init(endpoint, false, false)
  62. conn := Conn{cfg, &um, nil}
  63. uri := um.getURL("bucket", "object", "")
  64. req := &http.Request{
  65. Method: "PUT",
  66. URL: uri,
  67. Proto: "HTTP/1.1",
  68. ProtoMajor: 1,
  69. ProtoMinor: 1,
  70. Header: make(http.Header),
  71. Host: uri.Host,
  72. }
  73. req.Header.Set("Content-Type", "text/html")
  74. req.Header.Set("Date", "Thu, 17 Nov 2005 18:49:58 GMT")
  75. req.Header.Set("Host", endpoint)
  76. req.Header.Set("X-OSS-Meta-Your", "your")
  77. req.Header.Set("X-OSS-Meta-Author", "foo@bar.com")
  78. req.Header.Set("X-OSS-Magic", "abracadabra")
  79. req.Header.Set("Content-Md5", "ODBGOERFMDMzQTczRUY3NUE3NzA5QzdFNUYzMDQxNEM=")
  80. conn.signHeader(req, um.getResource("bucket", "object", ""))
  81. testLogger.Println("AUTHORIZATION:", req.Header.Get(HTTPHeaderAuthorization))
  82. }
  83. func (s *OssConnSuite) TestConnToolFunc(c *C) {
  84. err := checkRespCode(202, []int{})
  85. c.Assert(err, NotNil)
  86. err = checkRespCode(202, []int{404})
  87. c.Assert(err, NotNil)
  88. err = checkRespCode(202, []int{202, 404})
  89. c.Assert(err, IsNil)
  90. srvErr, err := serviceErrFromXML([]byte(""), 312, "")
  91. c.Assert(err, NotNil)
  92. c.Assert(srvErr.StatusCode, Equals, 0)
  93. srvErr, err = serviceErrFromXML([]byte("ABC"), 312, "")
  94. c.Assert(err, NotNil)
  95. c.Assert(srvErr.StatusCode, Equals, 0)
  96. srvErr, err = serviceErrFromXML([]byte("<Error></Error>"), 312, "")
  97. c.Assert(err, IsNil)
  98. c.Assert(srvErr.StatusCode, Equals, 312)
  99. unexpect := UnexpectedStatusCodeError{[]int{200}, 202}
  100. c.Assert(len(unexpect.Error()) > 0, Equals, true)
  101. c.Assert(unexpect.Got(), Equals, 202)
  102. }