conn_test.go 6.2 KB

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