conn_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package oss
  2. import (
  3. "net/http"
  4. "os"
  5. "strings"
  6. "time"
  7. . "gopkg.in/check.v1"
  8. )
  9. type OssConnSuite struct{}
  10. var _ = Suite(&OssConnSuite{})
  11. func (s *OssConnSuite) TestURLMarker(c *C) {
  12. um := urlMaker{}
  13. um.Init("docs.github.com", true, false)
  14. c.Assert(um.Type, Equals, urlTypeCname)
  15. c.Assert(um.Scheme, Equals, "http")
  16. c.Assert(um.NetLoc, Equals, "docs.github.com")
  17. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://docs.github.com/object?params")
  18. c.Assert(um.getURL("bucket", "object", "").String(), Equals, "http://docs.github.com/object")
  19. c.Assert(um.getURL("", "object", "").String(), Equals, "http://docs.github.com/object")
  20. var conn Conn
  21. conn.config = getDefaultOssConfig()
  22. conn.config.AuthVersion = AuthV1
  23. c.Assert(conn.getResource("bucket", "object", "subres"), Equals, "/bucket/object?subres")
  24. c.Assert(conn.getResource("bucket", "object", ""), Equals, "/bucket/object")
  25. c.Assert(conn.getResource("", "object", ""), Equals, "/")
  26. um.Init("https://docs.github.com", true, false)
  27. c.Assert(um.Type, Equals, urlTypeCname)
  28. c.Assert(um.Scheme, Equals, "https")
  29. c.Assert(um.NetLoc, Equals, "docs.github.com")
  30. um.Init("http://docs.github.com", true, false)
  31. c.Assert(um.Type, Equals, urlTypeCname)
  32. c.Assert(um.Scheme, Equals, "http")
  33. c.Assert(um.NetLoc, Equals, "docs.github.com")
  34. um.Init("docs.github.com:8080", false, true)
  35. c.Assert(um.Type, Equals, urlTypeAliyun)
  36. c.Assert(um.Scheme, Equals, "http")
  37. c.Assert(um.NetLoc, Equals, "docs.github.com:8080")
  38. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://bucket.docs.github.com:8080/object?params")
  39. c.Assert(um.getURL("bucket", "object", "").String(), Equals, "http://bucket.docs.github.com:8080/object")
  40. c.Assert(um.getURL("", "object", "").String(), Equals, "http://docs.github.com:8080/")
  41. c.Assert(conn.getResource("bucket", "object", "subres"), Equals, "/bucket/object?subres")
  42. c.Assert(conn.getResource("bucket", "object", ""), Equals, "/bucket/object")
  43. c.Assert(conn.getResource("", "object", ""), Equals, "/")
  44. um.Init("https://docs.github.com:8080", false, true)
  45. c.Assert(um.Type, Equals, urlTypeAliyun)
  46. c.Assert(um.Scheme, Equals, "https")
  47. c.Assert(um.NetLoc, Equals, "docs.github.com:8080")
  48. um.Init("127.0.0.1", false, true)
  49. c.Assert(um.Type, Equals, urlTypeIP)
  50. c.Assert(um.Scheme, Equals, "http")
  51. c.Assert(um.NetLoc, Equals, "127.0.0.1")
  52. um.Init("http://127.0.0.1", false, false)
  53. c.Assert(um.Type, Equals, urlTypeIP)
  54. c.Assert(um.Scheme, Equals, "http")
  55. c.Assert(um.NetLoc, Equals, "127.0.0.1")
  56. c.Assert(um.getURL("bucket", "object", "params").String(), Equals, "http://127.0.0.1/bucket/object?params")
  57. c.Assert(um.getURL("", "object", "params").String(), Equals, "http://127.0.0.1/?params")
  58. um.Init("https://127.0.0.1:8080", false, false)
  59. c.Assert(um.Type, Equals, urlTypeIP)
  60. c.Assert(um.Scheme, Equals, "https")
  61. c.Assert(um.NetLoc, Equals, "127.0.0.1:8080")
  62. um.Init("http://[2401:b180::dc]", false, false)
  63. c.Assert(um.Type, Equals, urlTypeIP)
  64. c.Assert(um.Scheme, Equals, "http")
  65. c.Assert(um.NetLoc, Equals, "[2401:b180::dc]")
  66. um.Init("https://[2401:b180::dc]:8080", false, false)
  67. c.Assert(um.Type, Equals, urlTypeIP)
  68. c.Assert(um.Scheme, Equals, "https")
  69. c.Assert(um.NetLoc, Equals, "[2401:b180::dc]:8080")
  70. }
  71. func (s *OssConnSuite) TestAuth(c *C) {
  72. endpoint := "https://github.com/"
  73. cfg := getDefaultOssConfig()
  74. cfg.AuthVersion = AuthV1
  75. um := urlMaker{}
  76. um.Init(endpoint, false, false)
  77. conn := Conn{cfg, &um, nil}
  78. uri := um.getURL("bucket", "object", "")
  79. req := &http.Request{
  80. Method: "PUT",
  81. URL: uri,
  82. Proto: "HTTP/1.1",
  83. ProtoMajor: 1,
  84. ProtoMinor: 1,
  85. Header: make(http.Header),
  86. Host: uri.Host,
  87. }
  88. req.Header.Set("Content-Type", "text/html")
  89. req.Header.Set("Date", "Thu, 17 Nov 2005 18:49:58 GMT")
  90. req.Header.Set("Host", endpoint)
  91. req.Header.Set("X-OSS-Meta-Your", "your")
  92. req.Header.Set("X-OSS-Meta-Author", "foo@bar.com")
  93. req.Header.Set("X-OSS-Magic", "abracadabra")
  94. req.Header.Set("Content-Md5", "ODBGOERFMDMzQTczRUY3NUE3NzA5QzdFNUYzMDQxNEM=")
  95. conn.signHeader(req, conn.getResource("bucket", "object", ""))
  96. testLogger.Println("AUTHORIZATION:", req.Header.Get(HTTPHeaderAuthorization))
  97. }
  98. func (s *OssConnSuite) TestConnToolFunc(c *C) {
  99. err := CheckRespCode(202, []int{})
  100. c.Assert(err, NotNil)
  101. err = CheckRespCode(202, []int{404})
  102. c.Assert(err, NotNil)
  103. err = CheckRespCode(202, []int{202, 404})
  104. c.Assert(err, IsNil)
  105. srvErr, err := serviceErrFromXML([]byte(""), 312, "")
  106. c.Assert(err, NotNil)
  107. c.Assert(srvErr.StatusCode, Equals, 0)
  108. srvErr, err = serviceErrFromXML([]byte("ABC"), 312, "")
  109. c.Assert(err, NotNil)
  110. c.Assert(srvErr.StatusCode, Equals, 0)
  111. srvErr, err = serviceErrFromXML([]byte("<Error></Error>"), 312, "")
  112. c.Assert(err, IsNil)
  113. c.Assert(srvErr.StatusCode, Equals, 312)
  114. unexpect := UnexpectedStatusCodeError{[]int{200}, 202}
  115. c.Assert(len(unexpect.Error()) > 0, Equals, true)
  116. c.Assert(unexpect.Got(), Equals, 202)
  117. fd, err := os.Open("../sample/BingWallpaper-2015-11-07.jpg")
  118. c.Assert(err, IsNil)
  119. fd.Close()
  120. var out ProcessObjectResult
  121. err = jsonUnmarshal(fd, &out)
  122. c.Assert(err, NotNil)
  123. }
  124. func (s *OssConnSuite) TestSignRtmpURL(c *C) {
  125. cfg := getDefaultOssConfig()
  126. um := urlMaker{}
  127. um.Init(endpoint, false, false)
  128. conn := Conn{cfg, &um, nil}
  129. //Anonymous
  130. channelName := "test-sign-rtmp-url"
  131. playlistName := "playlist.m3u8"
  132. expiration := time.Now().Unix() + 3600
  133. signedRtmpURL := conn.signRtmpURL(bucketName, channelName, playlistName, expiration)
  134. playURL := getPublishURL(bucketName, channelName)
  135. hasPrefix := strings.HasPrefix(signedRtmpURL, playURL)
  136. c.Assert(hasPrefix, Equals, true)
  137. //empty playlist name
  138. playlistName = ""
  139. signedRtmpURL = conn.signRtmpURL(bucketName, channelName, playlistName, expiration)
  140. playURL = getPublishURL(bucketName, channelName)
  141. hasPrefix = strings.HasPrefix(signedRtmpURL, playURL)
  142. c.Assert(hasPrefix, Equals, true)
  143. }
  144. func (s *OssConnSuite) TestGetRtmpSignedStr(c *C) {
  145. cfg := getDefaultOssConfig()
  146. um := urlMaker{}
  147. um.Init(endpoint, false, false)
  148. conn := Conn{cfg, &um, nil}
  149. akIf := conn.config.GetCredentials()
  150. //Anonymous
  151. channelName := "test-get-rtmp-signed-str"
  152. playlistName := "playlist.m3u8"
  153. expiration := time.Now().Unix() + 3600
  154. params := map[string]interface{}{}
  155. signedStr := conn.getRtmpSignedStr(bucketName, channelName, playlistName, expiration, akIf.GetAccessKeySecret(), params)
  156. c.Assert(signedStr, Equals, "")
  157. }