mailgunner 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,9 @@
1
1
  require 'minitest/autorun'
2
+ require 'webmock/minitest'
2
3
  require 'mocha/setup'
3
4
  require 'mailgunner'
4
- require 'faux'
5
5
  require 'json'
6
-
7
- class Net::HTTPGenericRequest
8
- def inspect
9
- if request_body_permitted?
10
- "<#{self.class.name} #{path} #{body}>"
11
- else
12
- "<#{self.class.name} #{path}>"
13
- end
14
- end
15
- end
6
+ require 'mail'
16
7
 
17
8
  describe 'Mailgunner::Client' do
18
9
  before do
@@ -20,6 +11,8 @@ describe 'Mailgunner::Client' do
20
11
 
21
12
  @api_key = 'xxx'
22
13
 
14
+ @base_url = "https://api:#{@api_key}@api.mailgun.net/v2"
15
+
23
16
  @client = Mailgunner::Client.new(domain: @domain, api_key: @api_key)
24
17
 
25
18
  @address = 'user@example.com'
@@ -27,12 +20,6 @@ describe 'Mailgunner::Client' do
27
20
  @encoded_address = 'user%40example.com'
28
21
  end
29
22
 
30
- def expect(request_class, arg)
31
- matcher = String === arg ? responds_with(:path, arg) : arg
32
-
33
- @client.http.expects(:request).with(all_of(instance_of(request_class), matcher)).returns(stub)
34
- end
35
-
36
23
  describe 'http method' do
37
24
  it 'returns a net http object that uses ssl' do
38
25
  @client.http.must_be_instance_of(Net::HTTP)
@@ -51,6 +38,12 @@ describe 'Mailgunner::Client' do
51
38
 
52
39
  Mailgunner::Client.new(api_key: @api_key).domain.must_equal(@domain)
53
40
  end
41
+
42
+ it 'defaults to nil if the MAILGUN_SMTP_LOGIN environment variable does not exist' do
43
+ ENV.delete('MAILGUN_SMTP_LOGIN')
44
+
45
+ Mailgunner::Client.new(api_key: @api_key).domain.must_be_nil
46
+ end
54
47
  end
55
48
 
56
49
  describe 'api_key method' do
@@ -67,7 +60,7 @@ describe 'Mailgunner::Client' do
67
60
 
68
61
  describe 'validate_address method' do
69
62
  it 'calls the address validate resource with the given email address and returns a response object' do
70
- expect(Net::HTTP::Get, "/v2/address/validate?address=#{@encoded_address}")
63
+ stub_request(:get, "#@base_url/address/validate?address=#@encoded_address")
71
64
 
72
65
  @client.validate_address(@address).must_be_instance_of(Mailgunner::Response)
73
66
  end
@@ -75,7 +68,7 @@ describe 'Mailgunner::Client' do
75
68
 
76
69
  describe 'parse_addresses method' do
77
70
  it 'calls the address parse resource with the given email addresses and returns a response object' do
78
- expect(Net::HTTP::Get, "/v2/address/parse?addresses=bob%40example.com%2Ceve%40example.com")
71
+ stub_request(:get, "#@base_url/address/parse?addresses=bob%40example.com%2Ceve%40example.com")
79
72
 
80
73
  @client.parse_addresses(['bob@example.com', 'eve@example.com']).must_be_instance_of(Mailgunner::Response)
81
74
  end
@@ -83,15 +76,15 @@ describe 'Mailgunner::Client' do
83
76
 
84
77
  describe 'send_message method' do
85
78
  it 'posts to the domain messages resource and returns a response object' do
86
- expect(Net::HTTP::Post, "/v2/#@domain/messages")
79
+ stub_request(:post, "#@base_url/#@domain/messages")
87
80
 
88
81
  @client.send_message({}).must_be_instance_of(Mailgunner::Response)
89
82
  end
90
83
 
91
84
  it 'encodes the message attributes' do
92
- expect(Net::HTTP::Post, responds_with(:body, "to=#@encoded_address"))
85
+ stub_request(:post, "#@base_url/#@domain/messages").with(body: "to=#@encoded_address")
93
86
 
94
- @client.add_domain({to: @address})
87
+ @client.send_message({to: @address})
95
88
  end
96
89
 
97
90
  it 'encodes the message attributes as multipart form data when sending attachments' do
@@ -99,9 +92,26 @@ describe 'Mailgunner::Client' do
99
92
  end
100
93
  end
101
94
 
95
+ describe 'send_mime method' do
96
+ before do
97
+ @mail = Mail.new({
98
+ to: 'alice@example.com',
99
+ from: 'bob@example.com',
100
+ subject: 'Test email',
101
+ body: 'This is a test email'
102
+ })
103
+ end
104
+
105
+ it 'posts to the domain messages resource and returns a response object' do
106
+ stub_request(:post, "#@base_url/#@domain/messages.mime")
107
+
108
+ @client.send_mime(@mail).must_be_instance_of(Mailgunner::Response)
109
+ end
110
+ end
111
+
102
112
  describe 'get_domains method' do
103
113
  it 'fetches the domains resource and returns a response object' do
104
- expect(Net::HTTP::Get, '/v2/domains')
114
+ stub_request(:get, "#@base_url/domains")
105
115
 
106
116
  @client.get_domains.must_be_instance_of(Mailgunner::Response)
107
117
  end
@@ -109,7 +119,7 @@ describe 'Mailgunner::Client' do
109
119
 
110
120
  describe 'get_domain method' do
111
121
  it 'fetches the domain resource and returns a response object' do
112
- expect(Net::HTTP::Get, "/v2/domains/#@domain")
122
+ stub_request(:get, "#@base_url/domains/#@domain")
113
123
 
114
124
  @client.get_domain(@domain).must_be_instance_of(Mailgunner::Response)
115
125
  end
@@ -117,13 +127,13 @@ describe 'Mailgunner::Client' do
117
127
 
118
128
  describe 'add_domain method' do
119
129
  it 'posts to the domains resource and returns a response object' do
120
- expect(Net::HTTP::Post, '/v2/domains')
130
+ stub_request(:post, "#@base_url/domains")
121
131
 
122
132
  @client.add_domain({}).must_be_instance_of(Mailgunner::Response)
123
133
  end
124
134
 
125
135
  it 'encodes the domain attributes' do
126
- expect(Net::HTTP::Post, responds_with(:body, "name=#@domain"))
136
+ stub_request(:post, "#@base_url/domains").with(body: "name=#@domain")
127
137
 
128
138
  @client.add_domain({name: @domain})
129
139
  end
@@ -131,13 +141,13 @@ describe 'Mailgunner::Client' do
131
141
 
132
142
  describe 'get_unsubscribes method' do
133
143
  it 'fetches the domain unsubscribes resource and returns a response object' do
134
- expect(Net::HTTP::Get, "/v2/#@domain/unsubscribes")
144
+ stub_request(:get, "#@base_url/#@domain/unsubscribes")
135
145
 
136
146
  @client.get_unsubscribes.must_be_instance_of(Mailgunner::Response)
137
147
  end
138
148
 
139
149
  it 'encodes skip and limit parameters' do
140
- expect(Net::HTTP::Get, "/v2/#@domain/unsubscribes?skip=1&limit=2")
150
+ stub_request(:get, "#@base_url/#@domain/unsubscribes?skip=1&limit=2")
141
151
 
142
152
  @client.get_unsubscribes(skip: 1, limit: 2)
143
153
  end
@@ -145,7 +155,7 @@ describe 'Mailgunner::Client' do
145
155
 
146
156
  describe 'get_unsubscribe method' do
147
157
  it 'fetches the unsubscribe resource with the given address and returns a response object' do
148
- expect(Net::HTTP::Get, "/v2/#@domain/unsubscribes/#@encoded_address")
158
+ stub_request(:get, "#@base_url/#@domain/unsubscribes/#@encoded_address")
149
159
 
150
160
  @client.get_unsubscribe(@address).must_be_instance_of(Mailgunner::Response)
151
161
  end
@@ -153,7 +163,7 @@ describe 'Mailgunner::Client' do
153
163
 
154
164
  describe 'delete_unsubscribe method' do
155
165
  it 'deletes the domain unsubscribe resource with the given address and returns a response object' do
156
- expect(Net::HTTP::Delete, "/v2/#@domain/unsubscribes/#@encoded_address")
166
+ stub_request(:delete, "#@base_url/#@domain/unsubscribes/#@encoded_address")
157
167
 
158
168
  @client.delete_unsubscribe(@address).must_be_instance_of(Mailgunner::Response)
159
169
  end
@@ -161,13 +171,13 @@ describe 'Mailgunner::Client' do
161
171
 
162
172
  describe 'add_unsubscribe method' do
163
173
  it 'posts to the domain unsubscribes resource and returns a response object' do
164
- expect(Net::HTTP::Post, "/v2/#@domain/unsubscribes")
174
+ stub_request(:post, "#@base_url/#@domain/unsubscribes")
165
175
 
166
176
  @client.add_unsubscribe({}).must_be_instance_of(Mailgunner::Response)
167
177
  end
168
178
 
169
179
  it 'encodes the unsubscribe attributes' do
170
- expect(Net::HTTP::Post, responds_with(:body, "address=#@encoded_address"))
180
+ stub_request(:post, "#@base_url/#@domain/unsubscribes").with(body: "address=#@encoded_address")
171
181
 
172
182
  @client.add_unsubscribe({address: @address})
173
183
  end
@@ -175,13 +185,13 @@ describe 'Mailgunner::Client' do
175
185
 
176
186
  describe 'get_complaints method' do
177
187
  it 'fetches the domain complaints resource and returns a response object' do
178
- expect(Net::HTTP::Get, "/v2/#@domain/complaints")
188
+ stub_request(:get, "#@base_url/#@domain/complaints")
179
189
 
180
190
  @client.get_complaints.must_be_instance_of(Mailgunner::Response)
181
191
  end
182
192
 
183
193
  it 'encodes skip and limit parameters' do
184
- expect(Net::HTTP::Get, "/v2/#@domain/complaints?skip=1&limit=2")
194
+ stub_request(:get, "#@base_url/#@domain/complaints?skip=1&limit=2")
185
195
 
186
196
  @client.get_complaints(skip: 1, limit: 2)
187
197
  end
@@ -189,7 +199,7 @@ describe 'Mailgunner::Client' do
189
199
 
190
200
  describe 'get_complaint method' do
191
201
  it 'fetches the complaint resource with the given address and returns a response object' do
192
- expect(Net::HTTP::Get, "/v2/#@domain/complaints/#@encoded_address")
202
+ stub_request(:get, "#@base_url/#@domain/complaints/#@encoded_address")
193
203
 
194
204
  @client.get_complaint(@address).must_be_instance_of(Mailgunner::Response)
195
205
  end
@@ -197,13 +207,13 @@ describe 'Mailgunner::Client' do
197
207
 
198
208
  describe 'add_complaint method' do
199
209
  it 'posts to the domain complaints resource and returns a response object' do
200
- expect(Net::HTTP::Post, "/v2/#@domain/complaints")
210
+ stub_request(:post, "#@base_url/#@domain/complaints")
201
211
 
202
212
  @client.add_complaint({}).must_be_instance_of(Mailgunner::Response)
203
213
  end
204
214
 
205
215
  it 'encodes the complaint attributes' do
206
- expect(Net::HTTP::Post, responds_with(:body, "address=#@encoded_address"))
216
+ stub_request(:post, "#@base_url/#@domain/complaints").with(body: "address=#@encoded_address")
207
217
 
208
218
  @client.add_complaint({address: @address})
209
219
  end
@@ -211,7 +221,7 @@ describe 'Mailgunner::Client' do
211
221
 
212
222
  describe 'delete_complaint method' do
213
223
  it 'deletes the domain complaint resource with the given address and returns a response object' do
214
- expect(Net::HTTP::Delete, "/v2/#@domain/complaints/#@encoded_address")
224
+ stub_request(:delete, "#@base_url/#@domain/complaints/#@encoded_address")
215
225
 
216
226
  @client.delete_complaint(@address).must_be_instance_of(Mailgunner::Response)
217
227
  end
@@ -219,13 +229,13 @@ describe 'Mailgunner::Client' do
219
229
 
220
230
  describe 'get_bounces method' do
221
231
  it 'fetches the domain bounces resource and returns a response object' do
222
- expect(Net::HTTP::Get, "/v2/#@domain/bounces")
232
+ stub_request(:get, "#@base_url/#@domain/bounces")
223
233
 
224
234
  @client.get_bounces.must_be_instance_of(Mailgunner::Response)
225
235
  end
226
236
 
227
237
  it 'encodes skip and limit parameters' do
228
- expect(Net::HTTP::Get, "/v2/#@domain/bounces?skip=1&limit=2")
238
+ stub_request(:get, "#@base_url/#@domain/bounces?skip=1&limit=2")
229
239
 
230
240
  @client.get_bounces(skip: 1, limit: 2)
231
241
  end
@@ -233,7 +243,7 @@ describe 'Mailgunner::Client' do
233
243
 
234
244
  describe 'get_bounce method' do
235
245
  it 'fetches the bounce resource with the given address and returns a response object' do
236
- expect(Net::HTTP::Get, "/v2/#@domain/bounces/#@encoded_address")
246
+ stub_request(:get, "#@base_url/#@domain/bounces/#@encoded_address")
237
247
 
238
248
  @client.get_bounce(@address).must_be_instance_of(Mailgunner::Response)
239
249
  end
@@ -241,13 +251,13 @@ describe 'Mailgunner::Client' do
241
251
 
242
252
  describe 'add_bounce method' do
243
253
  it 'posts to the domain bounces resource and returns a response object' do
244
- expect(Net::HTTP::Post, "/v2/#@domain/bounces")
254
+ stub_request(:post, "#@base_url/#@domain/bounces")
245
255
 
246
256
  @client.add_bounce({}).must_be_instance_of(Mailgunner::Response)
247
257
  end
248
258
 
249
259
  it 'encodes the bounce attributes' do
250
- expect(Net::HTTP::Post, responds_with(:body, "address=#@encoded_address"))
260
+ stub_request(:post, "#@base_url/#@domain/bounces").with(body: "address=#@encoded_address")
251
261
 
252
262
  @client.add_bounce({address: @address})
253
263
  end
@@ -255,7 +265,7 @@ describe 'Mailgunner::Client' do
255
265
 
256
266
  describe 'delete_bounce method' do
257
267
  it 'deletes the domain bounce resource with the given address and returns a response object' do
258
- expect(Net::HTTP::Delete, "/v2/#@domain/bounces/#@encoded_address")
268
+ stub_request(:delete, "#@base_url/#@domain/bounces/#@encoded_address")
259
269
 
260
270
  @client.delete_bounce(@address).must_be_instance_of(Mailgunner::Response)
261
271
  end
@@ -263,73 +273,47 @@ describe 'Mailgunner::Client' do
263
273
 
264
274
  describe 'get_stats method' do
265
275
  it 'fetches the domain stats resource and returns a response object' do
266
- expect(Net::HTTP::Get, "/v2/#@domain/stats")
276
+ stub_request(:get, "#@base_url/#@domain/stats")
267
277
 
268
278
  @client.get_stats.must_be_instance_of(Mailgunner::Response)
269
279
  end
270
280
 
271
281
  it 'encodes skip and limit parameters' do
272
- expect(Net::HTTP::Get, "/v2/#@domain/stats?skip=1&limit=2")
282
+ stub_request(:get, "#@base_url/#@domain/stats?skip=1&limit=2")
273
283
 
274
284
  @client.get_stats(skip: 1, limit: 2)
275
285
  end
276
286
 
277
287
  it 'encodes an event parameter with multiple values' do
278
- expect(Net::HTTP::Get, "/v2/#@domain/stats?event=sent&event=opened")
288
+ stub_request(:get, "#@base_url/#@domain/stats?event=sent&event=opened")
279
289
 
280
290
  @client.get_stats(event: %w(sent opened))
281
291
  end
282
292
  end
283
293
 
284
- describe 'get_log method' do
285
- before do
286
- Kernel.stubs(:warn)
287
- end
288
-
289
- it 'fetches the domain log resource and returns a response object' do
290
- expect(Net::HTTP::Get, "/v2/#@domain/log")
291
-
292
- @client.get_log.must_be_instance_of(Mailgunner::Response)
293
- end
294
-
295
- it 'encodes skip and limit parameters' do
296
- expect(Net::HTTP::Get, "/v2/#@domain/log?skip=1&limit=2")
297
-
298
- @client.get_log(skip: 1, limit: 2)
299
- end
300
-
301
- it 'emits a deprecation warning' do
302
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#get_log is deprecated/))
303
-
304
- @client.http.stubs(:request)
305
-
306
- @client.get_log
307
- end
308
- end
309
-
310
294
  describe 'get_events method' do
311
295
  it 'fetches the domain events resource and returns a response object' do
312
- expect(Net::HTTP::Get, "/v2/#@domain/events")
296
+ stub_request(:get, "#@base_url/#@domain/events")
313
297
 
314
298
  @client.get_events.must_be_instance_of(Mailgunner::Response)
315
299
  end
316
300
 
317
301
  it 'encodes optional parameters' do
318
- expect(Net::HTTP::Get, "/v2/#@domain/events?event=accepted&limit=10")
302
+ stub_request(:get, "#@base_url/#@domain/events?event=accepted&limit=10")
319
303
 
320
304
  @client.get_events(event: 'accepted', limit: 10)
321
305
  end
322
306
  end
323
307
 
324
308
  describe 'get_routes method' do
325
- it 'fetches the global routes resource and returns a response object' do
326
- expect(Net::HTTP::Get, '/v2/routes')
309
+ it 'fetches the routes resource and returns a response object' do
310
+ stub_request(:get, "#@base_url/routes")
327
311
 
328
312
  @client.get_routes.must_be_instance_of(Mailgunner::Response)
329
313
  end
330
314
 
331
315
  it 'encodes skip and limit parameters' do
332
- expect(Net::HTTP::Get, '/v2/routes?skip=1&limit=2')
316
+ stub_request(:get, "#@base_url/routes?skip=1&limit=2")
333
317
 
334
318
  @client.get_routes(skip: 1, limit: 2)
335
319
  end
@@ -337,7 +321,7 @@ describe 'Mailgunner::Client' do
337
321
 
338
322
  describe 'get_route method' do
339
323
  it 'fetches the route resource with the given id and returns a response object' do
340
- expect(Net::HTTP::Get, '/v2/routes/4f3bad2335335426750048c6')
324
+ stub_request(:get, "#@base_url/routes/4f3bad2335335426750048c6")
341
325
 
342
326
  @client.get_route('4f3bad2335335426750048c6').must_be_instance_of(Mailgunner::Response)
343
327
  end
@@ -345,13 +329,13 @@ describe 'Mailgunner::Client' do
345
329
 
346
330
  describe 'add_route method' do
347
331
  it 'posts to the routes resource and returns a response object' do
348
- expect(Net::HTTP::Post, '/v2/routes')
332
+ stub_request(:post, "#@base_url/routes")
349
333
 
350
334
  @client.add_route({}).must_be_instance_of(Mailgunner::Response)
351
335
  end
352
336
 
353
337
  it 'encodes the route attributes' do
354
- expect(Net::HTTP::Post, responds_with(:body, 'description=Example+route&priority=1'))
338
+ stub_request(:post, "#@base_url/routes").with(body: 'description=Example+route&priority=1')
355
339
 
356
340
  @client.add_route({description: 'Example route', priority: 1})
357
341
  end
@@ -359,13 +343,13 @@ describe 'Mailgunner::Client' do
359
343
 
360
344
  describe 'update_route method' do
361
345
  it 'updates the route resource with the given id and returns a response object' do
362
- expect(Net::HTTP::Put, '/v2/routes/4f3bad2335335426750048c6')
346
+ stub_request(:put, "#@base_url/routes/4f3bad2335335426750048c6")
363
347
 
364
348
  @client.update_route('4f3bad2335335426750048c6', {}).must_be_instance_of(Mailgunner::Response)
365
349
  end
366
350
 
367
351
  it 'encodes the route attributes' do
368
- expect(Net::HTTP::Put, responds_with(:body, 'priority=10'))
352
+ stub_request(:put, "#@base_url/routes/4f3bad2335335426750048c6").with(body: 'priority=10')
369
353
 
370
354
  @client.update_route('4f3bad2335335426750048c6', {priority: 10})
371
355
  end
@@ -373,119 +357,21 @@ describe 'Mailgunner::Client' do
373
357
 
374
358
  describe 'delete_route method' do
375
359
  it 'deletes the route resource with the given id and returns a response object' do
376
- expect(Net::HTTP::Delete, '/v2/routes/4f3bad2335335426750048c6')
360
+ stub_request(:delete, "#@base_url/routes/4f3bad2335335426750048c6")
377
361
 
378
362
  @client.delete_route('4f3bad2335335426750048c6').must_be_instance_of(Mailgunner::Response)
379
363
  end
380
364
  end
381
365
 
382
- describe 'get_mailboxes method' do
383
- before do
384
- Kernel.stubs(:warn)
385
- end
386
-
387
- it 'fetches the domain mailboxes resource and returns a response object' do
388
- expect(Net::HTTP::Get, "/v2/#@domain/mailboxes")
389
-
390
- @client.get_mailboxes.must_be_instance_of(Mailgunner::Response)
391
- end
392
-
393
- it 'encodes skip and limit parameters' do
394
- expect(Net::HTTP::Get, "/v2/#@domain/mailboxes?skip=1&limit=2")
395
-
396
- @client.get_mailboxes(skip: 1, limit: 2)
397
- end
398
-
399
- it 'emits a deprecation warning' do
400
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#get_mailboxes is deprecated/))
401
-
402
- @client.http.stubs(:request)
403
-
404
- @client.get_mailboxes
405
- end
406
- end
407
-
408
- describe 'add_mailbox method' do
409
- before do
410
- Kernel.stubs(:warn)
411
- end
412
-
413
- it 'posts to the domain mailboxes resource and returns a response object' do
414
- expect(Net::HTTP::Post, "/v2/#@domain/mailboxes")
415
-
416
- @client.add_mailbox({}).must_be_instance_of(Mailgunner::Response)
417
- end
418
-
419
- it 'encodes the mailbox attributes' do
420
- expect(Net::HTTP::Post, responds_with(:body, 'mailbox=user'))
421
-
422
- @client.add_mailbox({mailbox: 'user'})
423
- end
424
-
425
- it 'emits a deprecation warning' do
426
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#add_mailbox is deprecated/))
427
-
428
- @client.http.stubs(:request)
429
-
430
- @client.add_mailbox({})
431
- end
432
- end
433
-
434
- describe 'update_mailbox method' do
435
- before do
436
- Kernel.stubs(:warn)
437
- end
438
-
439
- it 'updates the mailbox resource and returns a response object' do
440
- expect(Net::HTTP::Put, "/v2/#@domain/mailboxes/user")
441
-
442
- @client.update_mailbox('user', {}).must_be_instance_of(Mailgunner::Response)
443
- end
444
-
445
- it 'encodes the mailbox attributes' do
446
- expect(Net::HTTP::Put, responds_with(:body, 'password=secret'))
447
-
448
- @client.update_mailbox('user', {password: 'secret'})
449
- end
450
-
451
- it 'emits a deprecation warning' do
452
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#update_mailbox is deprecated/))
453
-
454
- @client.http.stubs(:request)
455
-
456
- @client.update_mailbox('user', {})
457
- end
458
- end
459
-
460
- describe 'delete_mailbox method' do
461
- before do
462
- Kernel.stubs(:warn)
463
- end
464
-
465
- it 'deletes the domain mailbox resource with the given address and returns a response object' do
466
- expect(Net::HTTP::Delete, "/v2/#@domain/mailboxes/user")
467
-
468
- @client.delete_mailbox('user').must_be_instance_of(Mailgunner::Response)
469
- end
470
-
471
- it 'emits a deprecation warning' do
472
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#delete_mailbox is deprecated/))
473
-
474
- @client.http.stubs(:request)
475
-
476
- @client.delete_mailbox('user')
477
- end
478
- end
479
-
480
366
  describe 'get_campaigns method' do
481
367
  it 'fetches the domain campaigns resource and returns a response object' do
482
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns")
368
+ stub_request(:get, "#@base_url/#@domain/campaigns")
483
369
 
484
370
  @client.get_campaigns.must_be_instance_of(Mailgunner::Response)
485
371
  end
486
372
 
487
373
  it 'encodes skip and limit parameters' do
488
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns?skip=1&limit=2")
374
+ stub_request(:get, "#@base_url/#@domain/campaigns?skip=1&limit=2")
489
375
 
490
376
  @client.get_campaigns(skip: 1, limit: 2)
491
377
  end
@@ -493,7 +379,7 @@ describe 'Mailgunner::Client' do
493
379
 
494
380
  describe 'get_campaign method' do
495
381
  it 'fetches the campaign resource with the given id and returns a response object' do
496
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id")
382
+ stub_request(:get, "#@base_url/#@domain/campaigns/id")
497
383
 
498
384
  @client.get_campaign('id').must_be_instance_of(Mailgunner::Response)
499
385
  end
@@ -501,13 +387,13 @@ describe 'Mailgunner::Client' do
501
387
 
502
388
  describe 'add_campaign method' do
503
389
  it 'posts to the domain campaigns resource and returns a response object' do
504
- expect(Net::HTTP::Post, "/v2/#@domain/campaigns")
390
+ stub_request(:post, "#@base_url/#@domain/campaigns")
505
391
 
506
392
  @client.add_campaign({}).must_be_instance_of(Mailgunner::Response)
507
393
  end
508
394
 
509
395
  it 'encodes the campaign attributes' do
510
- expect(Net::HTTP::Post, responds_with(:body, 'id=id'))
396
+ stub_request(:post, "#@base_url/#@domain/campaigns").with(body: 'id=id')
511
397
 
512
398
  @client.add_campaign({id: 'id'})
513
399
  end
@@ -515,13 +401,13 @@ describe 'Mailgunner::Client' do
515
401
 
516
402
  describe 'update_campaign method' do
517
403
  it 'updates the campaign resource and returns a response object' do
518
- expect(Net::HTTP::Put, "/v2/#@domain/campaigns/id")
404
+ stub_request(:put, "#@base_url/#@domain/campaigns/id")
519
405
 
520
406
  @client.update_campaign('id', {}).must_be_instance_of(Mailgunner::Response)
521
407
  end
522
408
 
523
409
  it 'encodes the campaign attributes' do
524
- expect(Net::HTTP::Put, responds_with(:body, 'name=Example+Campaign'))
410
+ stub_request(:put, "#@base_url/#@domain/campaigns/id").with(body: 'name=Example+Campaign')
525
411
 
526
412
  @client.update_campaign('id', {name: 'Example Campaign'})
527
413
  end
@@ -529,7 +415,7 @@ describe 'Mailgunner::Client' do
529
415
 
530
416
  describe 'delete_campaign method' do
531
417
  it 'deletes the domain campaign resource with the given id and returns a response object' do
532
- expect(Net::HTTP::Delete, "/v2/#@domain/campaigns/id")
418
+ stub_request(:delete, "#@base_url/#@domain/campaigns/id")
533
419
 
534
420
  @client.delete_campaign('id').must_be_instance_of(Mailgunner::Response)
535
421
  end
@@ -537,13 +423,13 @@ describe 'Mailgunner::Client' do
537
423
 
538
424
  describe 'get_campaign_events method' do
539
425
  it 'fetches the domain campaign events resource and returns a response object' do
540
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/events")
426
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/events")
541
427
 
542
428
  @client.get_campaign_events('id').must_be_instance_of(Mailgunner::Response)
543
429
  end
544
430
 
545
431
  it 'encodes the optional parameters' do
546
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/events?country=US&limit=100")
432
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/events?country=US&limit=100")
547
433
 
548
434
  @client.get_campaign_events('id', country: 'US', limit: 100)
549
435
  end
@@ -551,13 +437,13 @@ describe 'Mailgunner::Client' do
551
437
 
552
438
  describe 'get_campaign_stats method' do
553
439
  it 'fetches the domain campaign stats resource and returns a response object' do
554
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/stats")
440
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/stats")
555
441
 
556
442
  @client.get_campaign_stats('id').must_be_instance_of(Mailgunner::Response)
557
443
  end
558
444
 
559
445
  it 'encodes the optional parameters' do
560
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/stats?groupby=dailyhour")
446
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/stats?groupby=dailyhour")
561
447
 
562
448
  @client.get_campaign_stats('id', groupby: 'dailyhour')
563
449
  end
@@ -565,13 +451,13 @@ describe 'Mailgunner::Client' do
565
451
 
566
452
  describe 'get_campaign_clicks method' do
567
453
  it 'fetches the domain campaign clicks resource and returns a response object' do
568
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/clicks")
454
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/clicks")
569
455
 
570
456
  @client.get_campaign_clicks('id').must_be_instance_of(Mailgunner::Response)
571
457
  end
572
458
 
573
459
  it 'encodes the optional parameters' do
574
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/clicks?groupby=month&limit=100")
460
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/clicks?groupby=month&limit=100")
575
461
 
576
462
  @client.get_campaign_clicks('id', groupby: 'month', limit: 100)
577
463
  end
@@ -579,13 +465,13 @@ describe 'Mailgunner::Client' do
579
465
 
580
466
  describe 'get_campaign_opens method' do
581
467
  it 'fetches the domain campaign opens resource and returns a response object' do
582
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/opens")
468
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/opens")
583
469
 
584
470
  @client.get_campaign_opens('id').must_be_instance_of(Mailgunner::Response)
585
471
  end
586
472
 
587
473
  it 'encodes the optional parameters' do
588
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/opens?groupby=month&limit=100")
474
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/opens?groupby=month&limit=100")
589
475
 
590
476
  @client.get_campaign_opens('id', groupby: 'month', limit: 100)
591
477
  end
@@ -593,13 +479,13 @@ describe 'Mailgunner::Client' do
593
479
 
594
480
  describe 'get_campaign_unsubscribes method' do
595
481
  it 'fetches the domain campaign unsubscribes resource and returns a response object' do
596
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/unsubscribes")
482
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/unsubscribes")
597
483
 
598
484
  @client.get_campaign_unsubscribes('id').must_be_instance_of(Mailgunner::Response)
599
485
  end
600
486
 
601
487
  it 'encodes the optional parameters' do
602
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/unsubscribes?groupby=month&limit=100")
488
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/unsubscribes?groupby=month&limit=100")
603
489
 
604
490
  @client.get_campaign_unsubscribes('id', groupby: 'month', limit: 100)
605
491
  end
@@ -607,27 +493,27 @@ describe 'Mailgunner::Client' do
607
493
 
608
494
  describe 'get_campaign_complaints method' do
609
495
  it 'fetches the domain campaign complaints resource and returns a response object' do
610
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/complaints")
496
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/complaints")
611
497
 
612
498
  @client.get_campaign_complaints('id').must_be_instance_of(Mailgunner::Response)
613
499
  end
614
500
 
615
501
  it 'encodes the optional parameters' do
616
- expect(Net::HTTP::Get, "/v2/#@domain/campaigns/id/complaints?groupby=month&limit=100")
502
+ stub_request(:get, "#@base_url/#@domain/campaigns/id/complaints?groupby=month&limit=100")
617
503
 
618
504
  @client.get_campaign_complaints('id', groupby: 'month', limit: 100)
619
505
  end
620
506
  end
621
507
 
622
508
  describe 'get_lists method' do
623
- it 'fetches the domain lists resource and returns a response object' do
624
- expect(Net::HTTP::Get, '/v2/lists')
509
+ it 'fetches the lists resource and returns a response object' do
510
+ stub_request(:get, "#@base_url/lists")
625
511
 
626
512
  @client.get_lists.must_be_instance_of(Mailgunner::Response)
627
513
  end
628
514
 
629
515
  it 'encodes skip and limit parameters' do
630
- expect(Net::HTTP::Get, '/v2/lists?skip=1&limit=2')
516
+ stub_request(:get, "#@base_url/lists?skip=1&limit=2")
631
517
 
632
518
  @client.get_lists(skip: 1, limit: 2)
633
519
  end
@@ -635,21 +521,21 @@ describe 'Mailgunner::Client' do
635
521
 
636
522
  describe 'get_list method' do
637
523
  it 'fetches the list resource with the given address and returns a response object' do
638
- expect(Net::HTTP::Get, '/v2/lists/developers%40mailgun.net')
524
+ stub_request(:get, "#@base_url/lists/developers%40mailgun.net")
639
525
 
640
526
  @client.get_list('developers@mailgun.net').must_be_instance_of(Mailgunner::Response)
641
527
  end
642
528
  end
643
529
 
644
530
  describe 'add_list method' do
645
- it 'posts to the domain lists resource and returns a response object' do
646
- expect(Net::HTTP::Post, '/v2/lists')
531
+ it 'posts to the lists resource and returns a response object' do
532
+ stub_request(:post, "#@base_url/lists")
647
533
 
648
534
  @client.add_list({}).must_be_instance_of(Mailgunner::Response)
649
535
  end
650
536
 
651
537
  it 'encodes the list attributes' do
652
- expect(Net::HTTP::Post, responds_with(:body, 'address=developers%40mailgun.net'))
538
+ stub_request(:post, "#@base_url/lists").with(body: 'address=developers%40mailgun.net')
653
539
 
654
540
  @client.add_list({address: 'developers@mailgun.net'})
655
541
  end
@@ -657,21 +543,21 @@ describe 'Mailgunner::Client' do
657
543
 
658
544
  describe 'update_list method' do
659
545
  it 'updates the list resource and returns a response object' do
660
- expect(Net::HTTP::Put, '/v2/lists/developers%40mailgun.net')
546
+ stub_request(:put, "#@base_url/lists/developers%40mailgun.net")
661
547
 
662
548
  @client.update_list('developers@mailgun.net', {}).must_be_instance_of(Mailgunner::Response)
663
549
  end
664
550
 
665
551
  it 'encodes the list attributes' do
666
- expect(Net::HTTP::Put, responds_with(:body, 'name=Example+list'))
552
+ stub_request(:put, "#@base_url/lists/developers%40mailgun.net").with(body: 'name=Example+list')
667
553
 
668
554
  @client.update_list('developers@mailgun.net', {name: 'Example list'})
669
555
  end
670
556
  end
671
557
 
672
558
  describe 'delete_list method' do
673
- it 'deletes the domain list resource with the given address and returns a response object' do
674
- expect(Net::HTTP::Delete, '/v2/lists/developers%40mailgun.net')
559
+ it 'deletes the list resource with the given address and returns a response object' do
560
+ stub_request(:delete, "#@base_url/lists/developers%40mailgun.net")
675
561
 
676
562
  @client.delete_list('developers@mailgun.net').must_be_instance_of(Mailgunner::Response)
677
563
  end
@@ -679,13 +565,13 @@ describe 'Mailgunner::Client' do
679
565
 
680
566
  describe 'get_list_members method' do
681
567
  it 'fetches the list members resource and returns a response object' do
682
- expect(Net::HTTP::Get, '/v2/lists/developers%40mailgun.net/members')
568
+ stub_request(:get, "#@base_url/lists/developers%40mailgun.net/members")
683
569
 
684
570
  @client.get_list_members('developers@mailgun.net').must_be_instance_of(Mailgunner::Response)
685
571
  end
686
572
 
687
573
  it 'encodes skip and limit parameters' do
688
- expect(Net::HTTP::Get, '/v2/lists/developers%40mailgun.net/members?skip=1&limit=2')
574
+ stub_request(:get, "#@base_url/lists/developers%40mailgun.net/members?skip=1&limit=2")
689
575
 
690
576
  @client.get_list_members('developers@mailgun.net', skip: 1, limit: 2)
691
577
  end
@@ -693,7 +579,7 @@ describe 'Mailgunner::Client' do
693
579
 
694
580
  describe 'get_list_member method' do
695
581
  it 'fetches the list member resource with the given address and returns a response object' do
696
- expect(Net::HTTP::Get, "/v2/lists/developers%40mailgun.net/members/#@encoded_address")
582
+ stub_request(:get, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address")
697
583
 
698
584
  @client.get_list_member('developers@mailgun.net', @address).must_be_instance_of(Mailgunner::Response)
699
585
  end
@@ -701,13 +587,13 @@ describe 'Mailgunner::Client' do
701
587
 
702
588
  describe 'add_list_member method' do
703
589
  it 'posts to the list members resource and returns a response object' do
704
- expect(Net::HTTP::Post, '/v2/lists/developers%40mailgun.net/members')
590
+ stub_request(:post, "#@base_url/lists/developers%40mailgun.net/members")
705
591
 
706
592
  @client.add_list_member('developers@mailgun.net', {}).must_be_instance_of(Mailgunner::Response)
707
593
  end
708
594
 
709
595
  it 'encodes the list attributes' do
710
- expect(Net::HTTP::Post, responds_with(:body, "address=#@encoded_address"))
596
+ stub_request(:post, "#@base_url/lists/developers%40mailgun.net/members").with(body: "address=#@encoded_address")
711
597
 
712
598
  @client.add_list_member('developers@mailgun.net', {address: @address})
713
599
  end
@@ -715,13 +601,13 @@ describe 'Mailgunner::Client' do
715
601
 
716
602
  describe 'update_list_member method' do
717
603
  it 'updates the list member resource with the given address and returns a response object' do
718
- expect(Net::HTTP::Put, "/v2/lists/developers%40mailgun.net/members/#@encoded_address")
604
+ stub_request(:put, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address")
719
605
 
720
606
  @client.update_list_member('developers@mailgun.net', @address, {}).must_be_instance_of(Mailgunner::Response)
721
607
  end
722
608
 
723
609
  it 'encodes the list member attributes' do
724
- expect(Net::HTTP::Put, responds_with(:body, 'subscribed=no'))
610
+ stub_request(:put, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address").with(body: 'subscribed=no')
725
611
 
726
612
  @client.update_list_member('developers@mailgun.net', @address, {subscribed: 'no'})
727
613
  end
@@ -729,7 +615,7 @@ describe 'Mailgunner::Client' do
729
615
 
730
616
  describe 'delete_list_member method' do
731
617
  it 'deletes the list member resource with the given address and returns a response object' do
732
- expect(Net::HTTP::Delete, "/v2/lists/developers%40mailgun.net/members/#@encoded_address")
618
+ stub_request(:delete, "#@base_url/lists/developers%40mailgun.net/members/#@encoded_address")
733
619
 
734
620
  @client.delete_list_member('developers@mailgun.net', @address).must_be_instance_of(Mailgunner::Response)
735
621
  end
@@ -737,142 +623,17 @@ describe 'Mailgunner::Client' do
737
623
 
738
624
  describe 'get_list_stats method' do
739
625
  it 'fetches the list stats resource and returns a response object' do
740
- expect(Net::HTTP::Get, '/v2/lists/developers%40mailgun.net/stats')
626
+ stub_request(:get, "#@base_url/lists/developers%40mailgun.net/stats")
741
627
 
742
628
  @client.get_list_stats('developers@mailgun.net').must_be_instance_of(Mailgunner::Response)
743
629
  end
744
630
  end
745
631
 
746
- describe 'json method' do
747
- it 'emits a deprecation warning and returns the standard library json module' do
748
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#json is deprecated/))
749
-
750
- Mailgunner::Client.new.json.must_equal(JSON)
751
- end
752
- end
753
-
754
- describe 'json equals method' do
755
- it 'emits a deprecation warning' do
756
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#json= is deprecated/))
757
-
758
- Mailgunner::Client.new.json = nil
759
- end
760
- end
761
-
762
632
  describe 'when initialized with a different json implementation' do
763
633
  it 'emits a deprecation warning' do
764
634
  Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client :json option is deprecated/))
765
635
 
766
636
  Mailgunner::Client.new(domain: @domain, api_key: @api_key, json: stub)
767
637
  end
768
-
769
- describe 'json method' do
770
- it 'emits a deprecation warning and returns the value passed to the constructor' do
771
- json = stub
772
-
773
- Kernel.expects(:warn).once
774
- Kernel.expects(:warn).with(regexp_matches(/Mailgunner::Client#json is deprecated/))
775
-
776
- Mailgunner::Client.new(json: json).json.must_equal(json)
777
- end
778
- end
779
- end
780
- end
781
-
782
- describe 'Mailgunner::Response' do
783
- before do
784
- @http_response = mock()
785
-
786
- @response = Mailgunner::Response.new(@http_response)
787
- end
788
-
789
- it 'delegates missing methods to the http response object' do
790
- @http_response.stubs(:code).returns('200')
791
-
792
- @response.code.must_equal('200')
793
- end
794
-
795
- describe 'ok query method' do
796
- it 'returns true if the status code is 200' do
797
- @http_response.expects(:code).returns('200')
798
-
799
- @response.ok?.must_equal(true)
800
- end
801
-
802
- it 'returns false otherwise' do
803
- @http_response.expects(:code).returns('400')
804
-
805
- @response.ok?.must_equal(false)
806
- end
807
- end
808
-
809
- describe 'client_error query method' do
810
- it 'returns true if the status code is 4xx' do
811
- @http_response.stubs(:code).returns(%w(400 401 402 404).sample)
812
-
813
- @response.client_error?.must_equal(true)
814
- end
815
-
816
- it 'returns false otherwise' do
817
- @http_response.stubs(:code).returns('200')
818
-
819
- @response.client_error?.must_equal(false)
820
- end
821
- end
822
-
823
- describe 'server_error query method' do
824
- it 'returns true if the status code is 5xx' do
825
- @http_response.stubs(:code).returns(%w(500 502 503 504).sample)
826
-
827
- @response.server_error?.must_equal(true)
828
- end
829
-
830
- it 'returns false otherwise' do
831
- @http_response.stubs(:code).returns('200')
832
-
833
- @response.server_error?.must_equal(false)
834
- end
835
- end
836
-
837
- describe 'json query method' do
838
- it 'returns true if the response has a json content type' do
839
- @http_response.expects(:[]).with('Content-Type').returns('application/json;charset=utf-8')
840
-
841
- @response.json?.must_equal(true)
842
- end
843
-
844
- it 'returns false otherwise' do
845
- @http_response.expects(:[]).with('Content-Type').returns('text/html')
846
-
847
- @response.json?.must_equal(false)
848
- end
849
- end
850
-
851
- describe 'object method' do
852
- it 'decodes the response body as json and returns a hash' do
853
- @http_response.expects(:body).returns('{"foo":"bar"}')
854
-
855
- @response.object.must_equal({'foo' => 'bar'})
856
- end
857
- end
858
- end
859
-
860
- describe 'Mailgunner::Response initialized with an alternative json implementation' do
861
- before do
862
- @json = faux(JSON)
863
-
864
- @http_response = stub
865
-
866
- @response = Mailgunner::Response.new(@http_response, :json => @json)
867
- end
868
-
869
- describe 'object method' do
870
- it 'uses the alternative json implementation to parse the response body' do
871
- @http_response.stubs(:body).returns(response_body = '{"foo":"bar"}')
872
-
873
- @json.expects(:parse).with(response_body)
874
-
875
- @response.object
876
- end
877
638
  end
878
639
  end