File: test_custom_headers.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 (78 lines) | stat: -rw-r--r-- 2,495 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
require 'minitest/autorun'
require 'yaml'
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
require 'aliyun/oss'
require 'zlib'
require_relative 'config'

class TestCustomHeaders < Minitest::Test
  def setup
    Aliyun::Common::Logging.set_log_level(Logger::DEBUG)
    client = Aliyun::OSS::Client.new(TestConf.creds)
    @bucket = client.get_bucket(TestConf.bucket)

    @prefix = "tests/custom_headers/"
  end

  def get_key(k)
    "#{@prefix}#{k}"
  end

  def test_custom_headers
    key = get_key('ruby')
    cache_control = 'max-age: 3600'
    @bucket.put_object(key, headers: {'cache-control' => cache_control})
    obj = @bucket.get_object(key)
    assert_equal cache_control, obj.headers[:cache_control]

    content_disposition = 'attachment; filename="fname.ext"'
    @bucket.put_object(
      key,
      headers: {'cache-control' => cache_control,
                'CONTENT-DISPOSITION' => content_disposition})
    obj = @bucket.get_object(key)
    assert_equal cache_control, obj.headers[:cache_control]
    assert_equal content_disposition, obj.headers[:content_disposition]

    content_encoding = 'deflate'
    expires = (Time.now + 3600).httpdate

    @bucket.put_object(
      key,
      headers: {'cache-control' => cache_control,
                'CONTENT-DISPOSITION' => content_disposition,
                'content-ENCODING' => content_encoding,
                'EXPIRES' => expires }) do |s|
      s << Zlib::Deflate.deflate('hello world')
    end

    content = ''
    obj = nil
    if @bucket.download_crc_enable
      assert_raises Aliyun::OSS::CrcInconsistentError do
        obj = @bucket.get_object(key) { |c| content << c }
      end
    else
      obj = @bucket.get_object(key) { |c| content << c }
      assert_equal 'hello world', content
      assert_equal cache_control, obj.headers[:cache_control]
      assert_equal content_disposition, obj.headers[:content_disposition]
      assert_equal content_encoding, obj.headers[:content_encoding]
      assert_equal expires, obj.headers[:expires]
    end
  end

  def test_headers_overwrite
    key = get_key('rails')
    @bucket.put_object(
      key,
      content_type: 'text/html',
      metas: {'hello' => 'world'},
      headers: {'content-type' => 'application/json',
                'x-oss-meta-hello' => 'bar'}) { |s| s << 'hello world' }
    obj = @bucket.get_object(key)

    assert_equal 'application/json', obj.headers[:content_type]
    assert_equal ({'hello' => 'bar'}), obj.metas
  end
end