simple-websocket-vcr 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6fcc25d2e22bc79b1a5359917af80ec0f10ae45e
4
- data.tar.gz: 2736b6895d6da6e42d372e478e92fbeb6bd7d14a
3
+ metadata.gz: aa78b72b8e5e63014cea6cb0b968f16cf788309a
4
+ data.tar.gz: ed9327485f96b7ef2833e992a6d535b944603d7d
5
5
  SHA512:
6
- metadata.gz: 6f1e61a02a7c1796460d423406c2d870035f74b761939983292423329ccca8ace7715576b38b45f6b3e2b2ec673bace43bb1045bd12484f54b5159eafbdc11b0
7
- data.tar.gz: f1a278cbb9e3dc882113e5a4e83bdf31f2a81af8fb20aaa2f72c17fd69c23d382ff00bf82eb301aae52a4f0eec4bc1c3482f3c9ba2a4339c373a9005607f2b29
6
+ metadata.gz: 216074d87f86830bc8f0838e7fa49c5dc53323a6bbc8ebc9c8d9fd40c600628e22696723f7cd38f40b6038a68fa3778e9643284933f2a8eac50541c4fa47acce
7
+ data.tar.gz: 79922e89ebecf2b24ffef5433e66bd75f58969b815c2132ad88574d2ba19ffda089ee0c157ceb1f2b80e2467dd03e601c725d755989f08fede0756579fa024b2
data/CHANGES.md CHANGED
@@ -3,7 +3,15 @@
3
3
  This document describes the relevant changes between releases of the
4
4
  _simple-websocket-vcr_ project.
5
5
 
6
- ### 0.0.5 (not released yet)
6
+ ### 0.0.7 (not released yet)
7
+
8
+ ### 0.0.6
9
+ * Reverse substitution for cassettes that have ERB. Basically, if we are recording and these options are passed: `erb: {some_key: some_value}, reverse_substitution: true`, then when the framework is about to store the `some_value` into cassette (JSON or YAML), the `<%= some_key =>` is stored instead of the value. So it allows to record the templates.
10
+ * increasing the code coverage
11
+
12
+ ### 0.0.5
13
+ * Now, there can be ERB in the cassettes (templating)
14
+ * YAML cassettes support
7
15
  * version is aligned with the VCR module so that coverall could do its job properly (https://travis-ci.org/Jiri-Kremser/simple-websocket-vcr/jobs/118142774#L215)
8
16
 
9
17
  ### 0.0.4
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/Jiri-Kremser/simple-websocket-vcr.png?branch=master)](https://travis-ci.org/Jiri-Kremser/simple-websocket-vcr)
4
4
  [![Coverage](https://coveralls.io/repos/github/Jiri-Kremser/simple-websocket-vcr/badge.svg?branch=master)](https://coveralls.io/github/Jiri-Kremser/simple-websocket-vcr?branch=master)
5
+ [![Code Climate](https://codeclimate.com/github/Jiri-Kremser/simple-websocket-vcr/badges/gpa.svg)](https://codeclimate.com/github/Jiri-Kremser/simple-websocket-vcr)
6
+ [![Gem Version](https://badge.fury.io/rb/simple-websocket-vcr.svg)](https://badge.fury.io/rb/simple-websocket-vcr)
5
7
 
6
8
 
7
9
  TODO: this
@@ -74,6 +74,8 @@ module WebSocketVCR
74
74
  # @param options [Hash] options for the cassette
75
75
  # @option options [Symbol] :record if set to :none there will be no recording
76
76
  # @option options [Symbol] :erb a sub-hash with variables used for ERB substitution in given cassette
77
+ # @option options [Boolean] :reverse_substitution if true, the values of :erb hash will be replaced by their names in
78
+ # the cassette. It's turned-off by default.
77
79
  def use_cassette(name, options = {})
78
80
  fail ArgumentError, '`VCR.use_cassette` requires a block.' unless block_given?
79
81
  self.cassette = Cassette.new(name, options)
@@ -27,7 +27,13 @@ module WebSocketVCR
27
27
 
28
28
  def next_session
29
29
  if recording?
30
- @sessions.push(@using_json ? RecordedJsonSession.new([]) : RecordedYamlSession.new([]))
30
+ erb_variables = @options[:reverse_substitution] ? @options[:erb] : nil
31
+ session = if @using_json
32
+ RecordedJsonSession.new([], erb_variables)
33
+ else
34
+ RecordedYamlSession.new([], erb_variables)
35
+ end
36
+ @sessions.push(session)
31
37
  @sessions.last
32
38
  else
33
39
  fail NoMoreSessionsError if @sessions.empty?
@@ -81,12 +87,18 @@ module WebSocketVCR
81
87
  class RecordedSession
82
88
  attr_reader :record_entries
83
89
 
84
- def initialize(entries)
90
+ def initialize(entries, erb_variables = nil)
85
91
  @record_entries = entries
92
+ @erb_variables = erb_variables
86
93
  end
87
94
 
88
95
  def store(entry)
89
- hash = entry.is_a?(RecordEntry) ? entry.attributes.map(&:to_s) : entry.map { |k, v| [k.to_s, v.to_s] }.to_h
96
+ hash = entry.is_a?(RecordEntry) ? entry.attributes.map(&:to_s) : Hash[entry.map { |k, v| [k.to_s, v.to_s] }]
97
+ if !hash['data'].nil? && !@erb_variables.nil?
98
+ @erb_variables.each do |k, v|
99
+ hash['data'].gsub! v.to_s, "<%= #{k} %>"
100
+ end
101
+ end
90
102
  @record_entries << hash
91
103
  end
92
104
 
@@ -1,3 +1,3 @@
1
1
  module WebSocketVCR
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -0,0 +1,7 @@
1
+ ---
2
+ websocket_interactions:
3
+ - - operation: read
4
+ type: text
5
+ data: <%= something %>={"sessionId":"_5_u_6hSDOVe0LlvGyx32CGR-f98iKwHQtjoQKy8"}
6
+ - operation: write
7
+ data: something_1
@@ -0,0 +1 @@
1
+ should_be_able_to_store_the_recording_with_real_communication_into_*
@@ -13,13 +13,14 @@ require 'json'
13
13
 
14
14
  describe 'VCR for WS' do
15
15
  HOST = 'localhost:8080'.freeze
16
+ ON_TRAVIS = ENV['TRAVIS'] == 'true'
16
17
 
17
- let(:example) do |e|
18
- e
18
+ RSpec.configure do |c|
19
+ c.filter_run_excluding skip: true
19
20
  end
20
21
 
21
- let(:should_sleep) do |_|
22
- !WebSocketVCR.cassette || WebSocketVCR.cassette.recording?
22
+ let(:example) do |e|
23
+ e
23
24
  end
24
25
 
25
26
  before(:each) do
@@ -35,7 +36,7 @@ describe 'VCR for WS' do
35
36
  c = WebSocket::Client::Simple.connect url do |client|
36
37
  client.on(:message, once: true, &:data)
37
38
  end
38
- sleep 1 if should_sleep
39
+ sleep 1 if WebSocketVCR.live?
39
40
 
40
41
  expect(c).not_to be nil
41
42
  expect(c.open?).to be true
@@ -47,11 +48,12 @@ describe 'VCR for WS' do
47
48
  c.hook_uris = [HOST]
48
49
  end
49
50
  WebSocketVCR.record(example, self) do
51
+ puts 'we are recording..' if WebSocketVCR.live?
50
52
  url = "ws://#{HOST}/hawkular/command-gateway/ui/ws"
51
53
  c = WebSocket::Client::Simple.connect url do |client|
52
54
  client.on(:message, once: true, &:data)
53
55
  end
54
- sleep 1 if should_sleep
56
+ sleep 1 if WebSocketVCR.live?
55
57
  c.send('something 1')
56
58
  c.send('something 2')
57
59
  c.send('something 3')
@@ -66,11 +68,11 @@ describe 'VCR for WS' do
66
68
  c = WebSocket::Client::Simple.connect url do |client|
67
69
  client.on(:message, once: true, &:data)
68
70
  end
69
- sleep 1 if should_sleep
71
+ sleep 1 if WebSocketVCR.live?
70
72
 
71
73
  expect(c).not_to be nil
72
74
  expect(c.open?).to be true
73
- sleep 1 if should_sleep
75
+ sleep 1 if WebSocketVCR.live?
74
76
  c.close
75
77
  expect(c.open?).to be false
76
78
  end
@@ -99,20 +101,20 @@ describe 'VCR for WS' do
99
101
  c = WebSocket::Client::Simple.connect url do |client|
100
102
  client.on(:message, once: true, &:data)
101
103
  end
102
- sleep 1 if should_sleep
104
+ sleep 1 if WebSocketVCR.live?
103
105
  c.send('something_1')
104
106
  c.on(:message, once: true, &:data)
105
- sleep 1 if should_sleep
107
+ sleep 1 if WebSocketVCR.live?
106
108
 
107
109
  c.send('something_2')
108
110
  c.on(:message, once: true, &:data)
109
- sleep 1 if should_sleep
111
+ sleep 1 if WebSocketVCR.live?
110
112
 
111
113
  expect(c).not_to be nil
112
114
  expect(c.open?).to be true
113
115
  c.close
114
116
  expect(c.open?).to be false
115
- sleep 1 if should_sleep
117
+ sleep 1 if WebSocketVCR.live?
116
118
  end
117
119
 
118
120
  it 'should record complex communications for json' do
@@ -243,7 +245,7 @@ describe 'VCR for WS' do
243
245
  expect(msg.data).to include(text1)
244
246
  end
245
247
  end
246
- sleep 1 if should_sleep
248
+ sleep 1 if WebSocketVCR.live?
247
249
  text2 ||= 'something_1'
248
250
  c.send(text2)
249
251
  c.on(:message, once: true) do |msg|
@@ -256,7 +258,7 @@ describe 'VCR for WS' do
256
258
  WebSocketVCR.configure do |c|
257
259
  c.hook_uris = [HOST]
258
260
  end
259
- WebSocketVCR.use_cassette(cassette_path, erb: { something: 11_223_344 }) do
261
+ WebSocketVCR.use_cassette(cassette_path, erb: { something: 11_223_344 }, record: :none) do
260
262
  test_substitution '11223344'
261
263
  end
262
264
  file_path = "#{WebSocketVCR.configuration.cassette_library_dir}#{cassette_path}.yml"
@@ -273,5 +275,84 @@ describe 'VCR for WS' do
273
275
  test_substitution 'world', 'hello'
274
276
  end
275
277
  end
278
+
279
+ it 'with :erb set to {something: 11223344}, and :reverse_substitution it should record the cassette as template' do
280
+ cassette_path = '/EXPLICIT/some_other_template'
281
+ WebSocketVCR.configure do |c|
282
+ c.hook_uris = [HOST]
283
+ end
284
+ WebSocketVCR.use_cassette(cassette_path, erb: { something: 'WelcomeResponse' }, reverse_substitution: true) do
285
+ test_substitution 'unlikely_string'
286
+ end
287
+ file_path = "#{WebSocketVCR.configuration.cassette_library_dir}#{cassette_path}.yml"
288
+ expect(File.readlines(file_path).grep(/<%= something %>/).size).to eq(1)
289
+ end
290
+ end
291
+
292
+ describe 'version' do
293
+ it 'should return the major, minor and micro components correctly' do
294
+ expect(WebSocketVCR.version).to include(WebSocketVCR.version.major.to_s)
295
+ expect(WebSocketVCR.version).to include(WebSocketVCR.version.minor.to_s)
296
+ expect(WebSocketVCR.version).to include(WebSocketVCR.version.patch.to_s)
297
+ end
298
+ end
299
+
300
+ def checks_for_echo_ws(file_path)
301
+ expect(File.readlines(file_path).grep(/hello/).size).to eq(2)
302
+ expect(File.readlines(file_path).grep(/how/).size).to eq(2)
303
+ expect(File.readlines(file_path).grep(/are/).size).to eq(2)
304
+ expect(File.readlines(file_path).grep(/you/).size).to eq(2)
305
+ expect(File.readlines(file_path).grep(/write/).size).to eq(4)
306
+ expect(File.readlines(file_path).grep(/read/).size).to eq(4)
307
+ expect(File.readlines(file_path).grep(/close/).size).to eq(1)
308
+ end
309
+
310
+ it 'should be able to store the recording with real communication into YAML', skip: !ON_TRAVIS do
311
+ WebSocketVCR.configure do |c|
312
+ c.hook_uris = ['echo.websocket.org']
313
+ end
314
+ WebSocketVCR.record(example, self) do
315
+ url = 'ws://echo.websocket.org'
316
+ c = WebSocket::Client::Simple.connect url do |client|
317
+ client.on(:message, &:data)
318
+ end
319
+ sleep 5 if WebSocketVCR.live?
320
+ c.send('hello')
321
+ c.send('how')
322
+ c.send('are')
323
+ c.send('you')
324
+ sleep 20 if WebSocketVCR.live?
325
+ c.close
326
+ expect(c.open?).to be false
327
+ end
328
+ # check that everything was recorded in the yaml file
329
+ cassette_name = 'VCR_for_WS/should_be_able_to_store_the_recording_with_real_communication_into_YAML.yml'
330
+ file_path = "#{WebSocketVCR.configuration.cassette_library_dir}/#{cassette_name}"
331
+ checks_for_echo_ws file_path
332
+ end
333
+
334
+ it 'should be able to store the recording with real communication into JSON', skip: !ON_TRAVIS do
335
+ WebSocketVCR.configure do |c|
336
+ c.hook_uris = ['echo.websocket.org']
337
+ c.json_cassettes = true
338
+ end
339
+ WebSocketVCR.record(example, self) do
340
+ url = 'ws://echo.websocket.org'
341
+ c = WebSocket::Client::Simple.connect url do |client|
342
+ client.on(:message, &:data)
343
+ end
344
+ sleep 5 if WebSocketVCR.live?
345
+ c.send('hello')
346
+ c.send('how')
347
+ c.send('are')
348
+ c.send('you')
349
+ sleep 20 if WebSocketVCR.live?
350
+ c.close
351
+ expect(c.open?).to be false
352
+ end
353
+ # check that everything was recorded in the json file
354
+ cassette_name = 'VCR_for_WS/should_be_able_to_store_the_recording_with_real_communication_into_JSON.json'
355
+ file_path = "#{WebSocketVCR.configuration.cassette_library_dir}/#{cassette_name}"
356
+ checks_for_echo_ws file_path
276
357
  end
277
358
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-websocket-vcr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jirka Kremser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-04 00:00:00.000000000 Z
11
+ date: 2016-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket-client-simple
@@ -149,8 +149,10 @@ files:
149
149
  - simple-websocket-vcr.gemspec
150
150
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_explicitly_specified_json_cassette.json
151
151
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_explicitly_specified_yaml_cassette.yml
152
+ - spec/fixtures/vcr_cassettes/EXPLICIT/some_other_template.yml
152
153
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_template.json
153
154
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_template.yml
155
+ - spec/fixtures/vcr_cassettes/VCR_for_WS/.gitignore
154
156
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child1.yml
155
157
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child2.yml
156
158
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child2_for_json.json
@@ -188,8 +190,10 @@ summary: simple_websocket_vcr is VCR add-on for websockets.
188
190
  test_files:
189
191
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_explicitly_specified_json_cassette.json
190
192
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_explicitly_specified_yaml_cassette.yml
193
+ - spec/fixtures/vcr_cassettes/EXPLICIT/some_other_template.yml
191
194
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_template.json
192
195
  - spec/fixtures/vcr_cassettes/EXPLICIT/some_template.yml
196
+ - spec/fixtures/vcr_cassettes/VCR_for_WS/.gitignore
193
197
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child1.yml
194
198
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child2.yml
195
199
  - spec/fixtures/vcr_cassettes/VCR_for_WS/automatically_picked_cassette_name_is_ok,_when_describing_parent_and_example_child2_for_json.json