@@ -126,39 +126,58 @@ def connect(*args)
126
126
expect ( Mysql2 ::Client ) . to respond_to ( :default_query_options )
127
127
end
128
128
129
- it "should be able to connect via SSL options" do
130
- ssl = @client . query "SHOW VARIABLES LIKE 'have_ssl'"
131
- ssl_uncompiled = ssl . any? { |x | x [ 'Value' ] == 'OFF' }
132
- pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon." ) if ssl_uncompiled
133
- ssl_disabled = ssl . any? { |x | x [ 'Value' ] == 'DISABLED' }
134
- pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon." ) if ssl_disabled
135
-
136
- # You may need to adjust the lines below to match your SSL certificate paths
137
- ssl_client = nil
138
- option_overrides = {
139
- 'host' => 'mysql2gem.example.com' , # must match the certificates
140
- :sslkey => '/etc/mysql/client-key.pem' ,
141
- :sslcert => '/etc/mysql/client-cert.pem' ,
142
- :sslca => '/etc/mysql/ca-cert.pem' ,
143
- :sslcipher => 'DHE-RSA-AES256-SHA' ,
144
- :sslverify => true ,
145
- }
146
- %i[ sslkey sslcert sslca ] . each do |item |
147
- unless File . exist? ( option_overrides [ item ] )
148
- pending ( "DON'T WORRY, THIS TEST PASSES - but #{ option_overrides [ item ] } does not exist." )
149
- break
129
+ context "SSL" do
130
+ before ( :example ) do
131
+ ssl = @client . query "SHOW VARIABLES LIKE 'have_ssl'"
132
+ ssl_uncompiled = ssl . any? { |x | x [ 'Value' ] == 'OFF' }
133
+ pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not compiled into your MySQL daemon." ) if ssl_uncompiled
134
+ ssl_disabled = ssl . any? { |x | x [ 'Value' ] == 'DISABLED' }
135
+ pending ( "DON'T WORRY, THIS TEST PASSES - but SSL is not enabled in your MySQL daemon." ) if ssl_disabled
136
+
137
+ %i[ sslkey sslcert sslca ] . each do |item |
138
+ unless File . exist? ( option_overrides [ item ] )
139
+ pending ( "DON'T WORRY, THIS TEST PASSES - but #{ option_overrides [ item ] } does not exist." )
140
+ break
141
+ end
150
142
end
151
143
end
152
- expect do
153
- ssl_client = new_client ( option_overrides )
154
- end . not_to raise_error
155
144
156
- results = Hash [ ssl_client . query ( 'SHOW STATUS WHERE Variable_name LIKE "Ssl_%"' ) . map { |x | x . values_at ( 'Variable_name' , 'Value' ) } ]
157
- expect ( results [ 'Ssl_cipher' ] ) . not_to be_empty
158
- expect ( results [ 'Ssl_version' ] ) . not_to be_empty
145
+ let ( :option_overrides ) do
146
+ {
147
+ 'host' => 'mysql2gem.example.com' , # must match the certificates
148
+ :sslkey => '/etc/mysql/client-key.pem' ,
149
+ :sslcert => '/etc/mysql/client-cert.pem' ,
150
+ :sslca => '/etc/mysql/ca-cert.pem' ,
151
+ :sslcipher => 'DHE-RSA-AES256-SHA' ,
152
+ :sslverify => true ,
153
+ }
154
+ end
155
+
156
+ let ( :ssl_client ) do
157
+ new_client ( option_overrides )
158
+ end
159
159
160
- expect ( ssl_client . ssl_cipher ) . not_to be_empty
161
- expect ( results [ 'Ssl_cipher' ] ) . to eql ( ssl_client . ssl_cipher )
160
+ %i[ disabled preferred required verify_ca verify_identity ] . each do |ssl_mode |
161
+ it "should set ssl_mode option #{ ssl_mode } " do
162
+ options = {
163
+ ssl_mode : ssl_mode ,
164
+ }
165
+ options . merge! ( option_overrides )
166
+ expect do
167
+ new_client ( options )
168
+ end . to_not output . to_stderr
169
+ end
170
+ end
171
+
172
+ it "should be able to connect via SSL options" do
173
+ # You may need to adjust the lines below to match your SSL certificate paths
174
+ results = Hash [ ssl_client . query ( 'SHOW STATUS WHERE Variable_name LIKE "Ssl_%"' ) . map { |x | x . values_at ( 'Variable_name' , 'Value' ) } ]
175
+ expect ( results [ 'Ssl_cipher' ] ) . not_to be_empty
176
+ expect ( results [ 'Ssl_version' ] ) . not_to be_empty
177
+
178
+ expect ( ssl_client . ssl_cipher ) . not_to be_empty
179
+ expect ( results [ 'Ssl_cipher' ] ) . to eql ( ssl_client . ssl_cipher )
180
+ end
162
181
end
163
182
164
183
def run_gc
0 commit comments