File: service_spec.rb

package info (click to toggle)
ruby-aliyun-sdk 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 792 kB
  • sloc: ruby: 7,909; ansic: 204; makefile: 4
file content (142 lines) | stat: -rw-r--r-- 4,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- encoding: utf-8 -*-

require 'spec_helper'
require 'yaml'
require 'nokogiri'

module Aliyun
  module OSS

    describe "Service" do
      before :all do
        @endpoint = 'oss.aliyuncs.com'
        @protocol = Protocol.new(
          Config.new(:endpoint => @endpoint,
                     :access_key_id => 'xxx', :access_key_secret => 'yyy'))

        @all_buckets = []
        (1..10).each do |i|
          name = "rubysdk-bucket-#{i.to_s.rjust(3, '0')}"
          @all_buckets << Bucket.new(
            :name => name,
            :location => 'oss-cn-hangzhou',
            :creation_time => Time.now)
        end
      end

      # 生成list_buckets返回的响应,包含bucket列表和more信息
      def mock_response(buckets, more)
        builder = Nokogiri::XML::Builder.new do |xml|
          xml.ListAllMyBucketsResult {
            xml.Owner {
              xml.ID 'owner_id'
              xml.DisplayName 'owner_name'
            }
            xml.Buckets {
              buckets.each do |b|
                xml.Bucket {
                  xml.Location b.location
                  xml.Name b.name
                  xml.CreationDate b.creation_time.to_s
                }
              end
            }

            unless more.empty?
              xml.Prefix more[:prefix]
              xml.Marker more[:marker]
              xml.MaxKeys more[:limit].to_s
              xml.NextMarker more[:next_marker]
              xml.IsTruncated more[:truncated]
            end
          }
        end

        builder.to_xml
      end

      context "List all buckets" do
        # 测试list_buckets正确地发送了HTTP请求
        it "should send correct request" do
          stub_request(:get, @endpoint)

          @protocol.list_buckets

          expect(WebMock).to have_requested(:get, @endpoint).
                              with(:body => nil, :query => {})
        end

        # 测试list_buckets正确地解析了list_buckets的返回
        it "should correctly parse response" do
          stub_request(:get, @endpoint).to_return(
            {:body => mock_response(@all_buckets, {})})

          buckets, more = @protocol.list_buckets
          bucket_names = buckets.map {|b| b.name}

          all_bucket_names = @all_buckets.map {|b| b.name}
          expect(bucket_names).to match_array(all_bucket_names)

          expect(more).to be_empty
        end
      end

      context "Paging buckets" do
        # 测试list_buckets的请求中包含prefix/marker/maxkeys等信息
        it "should set prefix/max-keys param" do
          prefix = 'rubysdk-bucket-00'
          marker = 'rubysdk-bucket-002'
          limit = 5

          stub_request(:get, @endpoint).with(
            :query => {'prefix' => prefix, 'marker' => marker, 'max-keys' => limit})

          @protocol.list_buckets(
            :prefix => prefix, :limit => limit, :marker => marker)

          expect(WebMock).to have_requested(:get, @endpoint).
            with(:body => nil,
                 :query => {'prefix' => prefix,
                            'marker' => marker,
                            'max-keys' => limit})
        end

        # 测试list_buckets正确地解析了HTTP响应,包含more信息
        it "should parse next marker" do
          prefix = 'rubysdk-bucket-00'
          marker = 'rubysdk-bucket-002'
          limit = 5
          # returns ['rubysdk-bucket-003', ..., 'rubysdk-bucket-007']
          return_buckets = @all_buckets[2, 5]
          next_marker = 'rubysdk-bucket-007'

          more = {:prefix => prefix, :marker => marker, :limit => limit,
                  :next_marker => next_marker, :truncated => true}

          stub_request(:get, @endpoint).with(
            :query => {'prefix' => prefix, 'marker' => marker, 'max-keys' => limit}
          ).to_return(
            {:body => mock_response(return_buckets, more)})

          buckets, more = @protocol.list_buckets(
                     :prefix => prefix,
                     :limit => limit,
                     :marker => marker)

          bucket_names = buckets.map {|b| b.name}
          return_bucket_names = return_buckets.map {|b| b.name}
          expect(bucket_names).to match_array(return_bucket_names)

          expect(more).to eq({
            :prefix => prefix,
            :marker => marker,
            :limit => limit,
            :next_marker => next_marker,
            :truncated => true})
        end

      end

    end # Bucket
  end # OSS
end # Aliyun