conn_test.go 4.9 KB

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