Skip to content

Commit

Permalink
add testcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Jun 4, 2020
1 parent b42eab7 commit 3db17df
Show file tree
Hide file tree
Showing 3 changed files with 421 additions and 3 deletions.
124 changes: 123 additions & 1 deletion spec/aliyun/oss/bucket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def mock_logging(opts)
if opts.enabled?
xml.LoggingEnabled {
xml.TargetBucket opts.target_bucket
xml.TargetPrefix opts.target_prefix
xml.TargetPrefix opts.target_prefix if opts.target_prefix
}
end
}
Expand Down Expand Up @@ -392,6 +392,19 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => mock_logging(logging_opts))
end

it "should enable logging without target prefix" do
query = {'logging' => nil}
stub_request(:put, request_path).with(:query => query)

logging_opts = BucketLogging.new(
:enable => true,
:target_bucket => 'target-bucket')
@protocol.put_bucket_logging(@bucket, logging_opts)

expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_logging(logging_opts))
end

it "should disable logging" do
query = {'logging' => nil}
stub_request(:put, request_path).with(:query => query)
Expand Down Expand Up @@ -420,6 +433,23 @@ def err(msg, reqid = '0000')
expect(logging.to_s).to eq(logging_opts.to_s)
end

it "should get logging without target-bucket" do
query = {'logging' => nil}
logging_opts = BucketLogging.new(
:enable => false,
:target_bucket => nil, :target_prefix => nil)

stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => mock_logging(logging_opts))

logging = @protocol.get_bucket_logging(@bucket)

expect(WebMock).to have_requested(:get, request_path)
.with(:query => query, :body => nil)
expect(logging.to_s).to eq(logging_opts.to_s)
end

it "should delete logging" do
query = {'logging' => nil}
stub_request(:delete, request_path).with(:query => query)
Expand All @@ -430,6 +460,18 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => nil)
end

it "should raise Exception when enable logging" do
query = {'logging' => nil}
stub_request(:put, request_path).with(:query => query)

logging_opts = BucketLogging.new(
:enable => true,
:target_bucket => nil, :target_prefix => nil)
expect {
@protocol.put_bucket_logging(@bucket, logging_opts)
}.to raise_error(ClientError)
end

it "should update website" do
query = {'website' => nil}
stub_request(:put, request_path).with(:query => query)
Expand All @@ -442,6 +484,18 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => mock_website(website_opts))
end

it "should update website with index only" do
query = {'website' => nil}
stub_request(:put, request_path).with(:query => query)

website_opts = BucketWebsite.new(
:enable => true, :index => 'index.html', :error => nil)
@protocol.put_bucket_website(@bucket, website_opts)

expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_website(website_opts))
end

it "should get website" do
query = {'website' => nil}
website_opts = BucketWebsite.new(
Expand All @@ -468,6 +522,18 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => nil)
end

it "should raise Exception when update website" do
query = {'website' => nil}
stub_request(:put, request_path).with(:query => query)

website_opts = BucketWebsite.new(
:enable => true, :index => nil)

expect {
@protocol.put_bucket_website(@bucket, website_opts)
}.to raise_error(ClientError)
end

it "should update referer" do
query = {'referer' => nil}
stub_request(:put, request_path).with(:query => query)
Expand Down Expand Up @@ -541,6 +607,44 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => nil)
end

it "should raise Exception when update lifecycle " do
query = {'lifecycle' => nil}
stub_request(:put, request_path).with(:query => query)

rules = (1..5).map do |i|
LifeCycleRule.new(
:id => i, :enable => i % 2 == 0, :prefix => "foo#{i}",
:expiry => 'invalid')
end

expect {
@protocol.put_bucket_lifecycle(@bucket, rules)
}.to raise_error(ClientError)
end

it "should raise Exception when get lifecycle " do
query = {'lifecycle' => nil}
body = '<LifecycleConfiguration>
<Rule>
<ID>delete after one day</ID>
<Prefix>logs/</Prefix>
<Status>Enabled</Status>
<Expiration>
<Days>1</Days>
<Date>2017-01-01T00:00:00.000Z</Date>
</Expiration>
</Rule>
</LifecycleConfiguration>'

stub_request(:get, request_path)
.with(:query => query)
.to_return(:body => body)

expect {
rules = @protocol.get_bucket_lifecycle(@bucket)
}.to raise_error(ClientError)
end

it "should set cors" do
query = {'cors' => nil}
stub_request(:put, request_path).with(:query => query)
Expand All @@ -558,6 +662,24 @@ def err(msg, reqid = '0000')
.with(:query => query, :body => mock_cors(rules))
end

it "should set cors with MaxAgeSeconds " do
query = {'cors' => nil}
stub_request(:put, request_path).with(:query => query)

rules = (1..5).map do |i|
CORSRule.new(
:allowed_origins => (1..3).map {|x| "origin-#{x}"},
:allowed_methods => ['PUT', 'GET'],
:allowed_headers => (1..3).map {|x| "header-#{x}"},
:expose_headers => (1..3).map {|x| "header-#{x}"},
:max_age_seconds => 5)
end
@protocol.set_bucket_cors(@bucket, rules)

expect(WebMock).to have_requested(:put, request_path)
.with(:query => query, :body => mock_cors(rules))
end

it "should get cors" do
query = {'cors' => nil}
return_rules = (1..5).map do |i|
Expand Down
Loading

0 comments on commit 3db17df

Please sign in to comment.