• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-ruby / #5867

18 Apr 2024 09:24PM UTC coverage: 92.724% (-4.8%) from 97.485%
#5867

push

github

ramya-stripe
Bump version to 11.2.0

10067 of 10857 relevant lines covered (92.72%)

258.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.57
/test/stripe/multipart_encoder_test.rb
1
# frozen_string_literal: true
2

3
require File.expand_path("../test_helper", __dir__)
1✔
4

5
module Stripe
1✔
6
  class MultipartEncoderTest < Test::Unit::TestCase
1✔
7
    should "multipart encode parameters" do
1✔
8
      Tempfile.create("image.jpg") do |f|
1✔
9
        f.write "file-content"
1✔
10
        f.flush
1✔
11
        f.rewind
1✔
12

13
        encoder = MultipartEncoder.new
1✔
14
        encoder.encode(
1✔
15
          file: f,
16
          other_param: "other-param-content"
17
        )
18
        encoder.close
1✔
19
        body = encoder.body
1✔
20

21
        assert_equal <<~BODY.rstrip, body
2✔
22
          --#{encoder.boundary}\r
×
23
          Content-Disposition: form-data; name="file"; filename="#{::File.basename(f.path)}"\r
1✔
24
          Content-Type: application/octet-stream\r
25
          \r
26
          file-content\r
27
          --#{encoder.boundary}\r
×
28
          Content-Disposition: form-data; name="other_param"\r
29
          \r
30
          other-param-content\r
31
          --#{encoder.boundary}--
×
32
        BODY
33
      end
34
    end
35

36
    should "encode file-like objects" do
1✔
37
      klass = Class.new do
1✔
38
        def read
1✔
39
          "klass-read-content"
1✔
40
        end
41
      end
42

43
      encoder = MultipartEncoder.new
1✔
44
      encoder.encode(
1✔
45
        file_like: klass.new
×
46
      )
47
      encoder.close
1✔
48
      body = encoder.body
1✔
49

50
      assert_equal <<~BODY.rstrip, body
1✔
51
        --#{encoder.boundary}\r
×
52
        Content-Disposition: form-data; name="file_like"; filename="blob"\r
53
        Content-Type: application/octet-stream\r
54
        \r
55
        klass-read-content\r
56
        --#{encoder.boundary}--
×
57
      BODY
58
    end
59

60
    should "escape quotes and line break characters in parameter names" do
1✔
61
      encoder = MultipartEncoder.new
1✔
62
      encoder.encode(
1✔
63
        %("quoted\n\r") => "content"
64
      )
65
      encoder.close
1✔
66
      body = encoder.body
1✔
67

68
      assert_equal <<~BODY.rstrip, body
1✔
69
        --#{encoder.boundary}\r
×
70
        Content-Disposition: form-data; name="%22quoted  %22"\r
71
        \r
72
        content\r
73
        --#{encoder.boundary}--
×
74
      BODY
75
    end
76

77
    context ".encode" do
1✔
78
      should "provide an easy encoding shortcut" do
1✔
79
        body, content_type = MultipartEncoder.encode(
1✔
80
          param: "content"
81
        )
82
        assert_include body, %(Content-Disposition: form-data; name="param")
1✔
83
        assert_include content_type, "#{MultipartEncoder::MULTIPART_FORM_DATA}; boundary="
1✔
84
      end
85
    end
86

87
    context "#body" do
1✔
88
      should "error if not yet closed" do
1✔
89
        encoder = MultipartEncoder.new
1✔
90

91
        e = assert_raises RuntimeError do
1✔
92
          encoder.body
1✔
93
        end
94
        assert_equal "object must be closed before getting body", e.message
1✔
95
      end
96
    end
97

98
    context "#close" do
1✔
99
      should "error if closed twice" do
1✔
100
        encoder = MultipartEncoder.new
1✔
101
        encoder.close
1✔
102

103
        e = assert_raises RuntimeError do
1✔
104
          encoder.close
1✔
105
        end
106
        assert_equal "object already closed", e.message
1✔
107
      end
108
    end
109

110
    context "#content_type" do
1✔
111
      should "produce a content type containing boundary" do
1✔
112
        encoder = MultipartEncoder.new
1✔
113
        assert_equal "#{MultipartEncoder::MULTIPART_FORM_DATA}; boundary=#{encoder.boundary}",
2✔
114
                     encoder.content_type
×
115
      end
116
    end
117

118
    context "#encode" do
1✔
119
      should "error if already closed" do
1✔
120
        encoder = MultipartEncoder.new
1✔
121
        encoder.close
1✔
122

123
        e = assert_raises RuntimeError do
1✔
124
          encoder.encode(param: "content")
1✔
125
        end
126
        assert_equal "no more parameters can be written to closed object", e.message
1✔
127
      end
128
    end
129
  end
130
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc