dns_mock 0.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24eced6efdb48a74315875839beb3af4e693962a3eb1c5ea3b82f9de67ab5bf4
4
- data.tar.gz: e11a83bb5ba9a8cf5f5ba0d6d98689a6886b9954b4ba0f33cfe3de026671a905
3
+ metadata.gz: 064a09d83281b9b85beb9c30435f5db5bd68b5658389ff9ac8776aa23a148bff
4
+ data.tar.gz: d9bb631674d58c3588c76deb8adedd38fd5dcef17cc15e38ad7185e9b1af11cb
5
5
  SHA512:
6
- metadata.gz: 5f3863ee0037d22f13e2a0b1e3126193cf485447b3f60edf998ff631204f46fe7ac443f1fc9431431e10a2577392000b388c9b110d26e13535524e6d53e18f66
7
- data.tar.gz: 5c795bd05c4eb4502a248f483c01778aaefac1fe32a1d937364518c6e3299414145dd973ca2ffeb6688c940217be4fc11e6e321d9f1da0ef5bce6e1c03f4e025
6
+ metadata.gz: dfa19e12835b63f6637f41daa3a7c4fe2c0cecf5b32a6a5e0348398b4201e8e0ad7d2468a068e3c735e438a6e429b82878dc9b9c56c04cdea075cfa0c99b9c3f
7
+ data.tar.gz: ed40dcd5e44181d3df023628cbcaef4478dfa152ed2f3e2434561ef85f628a34157274dfe811a27be78b63f80db2719106438ccd8325d436e412dd95c19e8c49
data/.codeclimate.yml CHANGED
@@ -7,7 +7,7 @@ checks:
7
7
  plugins:
8
8
  rubocop:
9
9
  enabled: true
10
- channel: rubocop-1-8
10
+ channel: rubocop-1-11
11
11
 
12
12
  reek:
13
13
  enabled: true
data/.reek.yml CHANGED
@@ -21,6 +21,8 @@ detectors:
21
21
  - DnsMock::ServerHelper#start_random_server
22
22
  - DnsMock::ServerHelper#stop_all_running_servers
23
23
  - DnsMock::Server::RecordsDictionaryBuilder#rdns_lookup_prefix
24
+ - DnsMock::ContextGeneratorHelper#random_port_number
25
+ - DnsMock::TestFramework::RSpec::Helper#dns_mock_server
24
26
 
25
27
  ControlParameter:
26
28
  exclude:
@@ -45,3 +47,13 @@ detectors:
45
47
  LongParameterList:
46
48
  exclude:
47
49
  - DnsMock::Server#initialize
50
+ - DnsMock#start_server
51
+
52
+ BooleanParameter:
53
+ exclude:
54
+ - DnsMock::Server#initialize
55
+ - DnsMock#start_server
56
+
57
+ NilCheck:
58
+ exclude:
59
+ - DnsMock::Record::Builder::Mx#build
data/.rubocop.yml CHANGED
@@ -118,6 +118,12 @@ Style/EndlessMethod:
118
118
  Style/HashExcept:
119
119
  Enabled: true
120
120
 
121
+ Style/IfWithBooleanLiteralBranches:
122
+ Enabled: true
123
+
124
+ Style/HashConversion:
125
+ Enabled: true
126
+
121
127
  Layout/LineLength:
122
128
  Max: 150
123
129
 
@@ -203,6 +209,21 @@ Lint/RedundantDirGlobSort:
203
209
  Lint/UnmodifiedReduceAccumulator:
204
210
  Enabled: true
205
211
 
212
+ Lint/NumberedParameterAssignment:
213
+ Enabled: true
214
+
215
+ Lint/OrAssignmentToConstant:
216
+ Enabled: true
217
+
218
+ Lint/SymbolConversion:
219
+ Enabled: true
220
+
221
+ Lint/TripleQuotes:
222
+ Enabled: true
223
+
224
+ Gemspec/DateAssignment:
225
+ Enabled: true
226
+
206
227
  Performance/AncestorsInclude:
207
228
  Enabled: true
208
229
 
@@ -242,6 +263,12 @@ Performance/MethodObjectAsBlock:
242
263
  Performance/Sum:
243
264
  Enabled: true
244
265
 
266
+ Performance/RedundantEqualityComparisonBlock:
267
+ Enabled: true
268
+
269
+ Performance/RedundantSplitRegexpArgument:
270
+ Enabled: true
271
+
245
272
  RSpec/ExampleLength:
246
273
  Enabled: false
247
274
 
data/CHANGELOG.md CHANGED
@@ -2,6 +2,136 @@
2
2
 
3
3
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4
4
 
5
+ ## [1.2.1] - 2021-03-23
6
+
7
+ ### Changed
8
+
9
+ - Updated gem development dependencies
10
+ - Updated rubocop/codeclimate config
11
+ - Updated gem documentation
12
+ - Updated tests
13
+
14
+ ## [1.2.0] - 2021-02-04
15
+
16
+ ### Ability to specify MX record priority
17
+
18
+ Added ability to specify custom priority of MX record if it needed. Now it impossible to define null or backup MX records. Please note, if you haven't specified a priority of MX record, it will be assigned automatically. MX records builder is assigning priority with step 10 from first item of defined MX records array.
19
+
20
+ ```ruby
21
+ records = {
22
+ 'example.com' => {
23
+ mx: %w[.:0 mx1.domain.com:10 mx2.domain.com:10 mx3.domain.com] # .:0 - null MX record
24
+ }
25
+ }
26
+
27
+ DnsMock.start_server(records: records)
28
+ ```
29
+
30
+ ```bash
31
+ dig @localhost -p 5300 MX example.com
32
+ ```
33
+
34
+ ```
35
+ ; <<>> DiG 9.10.6 <<>> @localhost -p 5300 MX example.com
36
+
37
+ ;; ANSWER SECTION:
38
+ example.com. 1 IN MX 0 .
39
+ example.com. 1 IN MX 10 mx1.domain.com.
40
+ example.com. 1 IN MX 10 mx2.domain.com.
41
+ example.com. 1 IN MX 40 mx3.domain.com.
42
+
43
+ ;; Query time: 0 msec
44
+ ;; SERVER: 127.0.0.1#5300(127.0.0.1)
45
+ ;; WHEN: Wed Feb 03 20:19:51 EET 2021
46
+ ;; MSG SIZE rcvd: 102
47
+ ```
48
+
49
+ ## [1.1.0] - 2021-02-01
50
+
51
+ ### RSpec native support
52
+
53
+ Added DnsMock helper which can simplify integration with RSpec.
54
+
55
+ ```ruby
56
+ # spec/support/config/dns_mock.rb
57
+ require 'dns_mock/test_framework/rspec'
58
+
59
+ RSpec.configure do |config|
60
+ config.include DnsMock::TestFramework::RSpec::Helper
61
+ end
62
+
63
+ # your awesome first_a_record_spec.rb
64
+ RSpec.describe FirstARecord do
65
+ subject(:service) do
66
+ described_class.call(
67
+ hostname,
68
+ dns_gateway_host: 'localhost',
69
+ dns_gateway_port: dns_mock_server.port
70
+ )
71
+ end
72
+
73
+ let(:hostname) { 'example.com' }
74
+ let(:first_a_record) { '1.2.3.4' }
75
+ let(:records) { { hostname => { a: [first_a_record] } } }
76
+
77
+ before { dns_mock_server.assign_mocks(records) }
78
+
79
+ it { is_expected.to eq(first_a_record) }
80
+ end
81
+ ```
82
+
83
+ ## [1.0.0] - 2021-01-29
84
+
85
+ ### Configurable record not found behaviour
86
+
87
+ Added configurable strategy for record not found case. By default it won't raise an exception when DNS record not found in mocked records dictionary:
88
+
89
+ ```ruby
90
+ DnsMock.start_server(port: 5300)
91
+ ```
92
+
93
+ ```bash
94
+ dig @localhost -p 5300 A example.com
95
+ ```
96
+
97
+ ```
98
+ ; <<>> DiG 9.10.6 <<>> @localhost -p 5300 A example.com
99
+ ;; Got answer:
100
+ ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38632
101
+ ;; flags: rd; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1
102
+ ;; WARNING: recursion requested but not available
103
+
104
+ ;; OPT PSEUDOSECTION:
105
+ ; EDNS: version: 0, flags:; udp: 4096
106
+ ;; QUESTION SECTION:
107
+ ;example.com. IN A
108
+
109
+ ;; Query time: 0 msec
110
+ ;; SERVER: 127.0.0.1#5300(127.0.0.1)
111
+ ;; WHEN: Fri Jan 29 08:21:30 EET 2021
112
+ ;; MSG SIZE rcvd: 40
113
+ ```
114
+
115
+ If you want raise an exception when record not found, just start `DnsMock` with `exception_if_not_found: true` option:
116
+
117
+ ```ruby
118
+ DnsMock.start_server(exception_if_not_found: true)
119
+ ```
120
+
121
+ ### Changed
122
+
123
+ - Updated `DnsMock.start_server`
124
+ - Updated `DnsMock::Server`
125
+ - Updated `DnsMock::Response::Message`
126
+ - Updated `DnsMock::Response::Answer`
127
+ - Updated gem version, readme
128
+
129
+ ## [0.2.1] - 2021-01-27
130
+
131
+ ### Fixed RDNS lookup representation
132
+
133
+ Fixed RDNS lookup representatin for IP address in PTR record feature.
134
+
5
135
  ## [0.2.0] - 2021-01-26
6
136
 
7
137
  ### PTR record support
data/Gemfile.lock CHANGED
@@ -1,30 +1,30 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dns_mock (0.2.0)
4
+ dns_mock (1.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.1)
10
- bundler-audit (0.7.0.1)
9
+ ast (2.4.2)
10
+ bundler-audit (0.8.0)
11
11
  bundler (>= 1.2.0, < 3)
12
- thor (>= 0.18, < 2)
12
+ thor (~> 1.0)
13
13
  byebug (11.1.3)
14
14
  childprocess (4.0.0)
15
15
  coderay (1.1.3)
16
16
  colorize (0.8.1)
17
- concurrent-ruby (1.1.7)
17
+ concurrent-ruby (1.1.8)
18
18
  diff-lcs (1.4.4)
19
19
  dnsruby (1.61.5)
20
20
  simpleidn (~> 0.1)
21
21
  docile (1.3.5)
22
- faker (2.15.1)
22
+ faker (2.17.0)
23
23
  i18n (>= 1.6, < 2)
24
- fasterer (0.8.3)
24
+ fasterer (0.9.0)
25
25
  colorize (~> 0.7)
26
26
  ruby_parser (>= 3.14.1)
27
- i18n (1.8.7)
27
+ i18n (1.8.9)
28
28
  concurrent-ruby (~> 1.0)
29
29
  iniparse (1.5.0)
30
30
  json (2.5.1)
@@ -42,7 +42,7 @@ GEM
42
42
  pry-byebug (3.9.0)
43
43
  byebug (~> 11.0)
44
44
  pry (~> 0.13.0)
45
- psych (3.3.0)
45
+ psych (3.3.1)
46
46
  rainbow (3.0.0)
47
47
  rake (13.0.3)
48
48
  reek (6.0.3)
@@ -50,7 +50,7 @@ GEM
50
50
  parser (~> 3.0.0)
51
51
  psych (~> 3.1)
52
52
  rainbow (>= 2.0, < 4.0)
53
- regexp_parser (2.0.3)
53
+ regexp_parser (2.1.1)
54
54
  rexml (3.2.4)
55
55
  rspec (3.10.0)
56
56
  rspec-core (~> 3.10.0)
@@ -65,11 +65,11 @@ GEM
65
65
  rspec-expectations (3.10.1)
66
66
  diff-lcs (>= 1.2.0, < 2.0)
67
67
  rspec-support (~> 3.10.0)
68
- rspec-mocks (3.10.1)
68
+ rspec-mocks (3.10.2)
69
69
  diff-lcs (>= 1.2.0, < 2.0)
70
70
  rspec-support (~> 3.10.0)
71
- rspec-support (3.10.1)
72
- rubocop (1.8.1)
71
+ rspec-support (3.10.2)
72
+ rubocop (1.11.0)
73
73
  parallel (~> 1.10)
74
74
  parser (>= 3.0.0.0)
75
75
  rainbow (>= 2.2.2, < 4.0)
@@ -78,12 +78,12 @@ GEM
78
78
  rubocop-ast (>= 1.2.0, < 2.0)
79
79
  ruby-progressbar (~> 1.7)
80
80
  unicode-display_width (>= 1.4.0, < 3.0)
81
- rubocop-ast (1.4.0)
81
+ rubocop-ast (1.4.1)
82
82
  parser (>= 2.7.1.5)
83
- rubocop-performance (1.9.2)
83
+ rubocop-performance (1.10.2)
84
84
  rubocop (>= 0.90.0, < 2.0)
85
85
  rubocop-ast (>= 0.4.0)
86
- rubocop-rspec (2.1.0)
86
+ rubocop-rspec (2.2.0)
87
87
  rubocop (~> 1.0)
88
88
  rubocop-ast (>= 1.1.0)
89
89
  ruby-progressbar (1.11.0)
@@ -97,7 +97,7 @@ GEM
97
97
  simplecov-html (0.10.2)
98
98
  simpleidn (0.2.1)
99
99
  unf (~> 0.1.4)
100
- thor (1.0.1)
100
+ thor (1.1.0)
101
101
  unf (0.1.4)
102
102
  unf_ext
103
103
  unf_ext (0.0.7.7)
@@ -108,19 +108,19 @@ PLATFORMS
108
108
 
109
109
  DEPENDENCIES
110
110
  bundler (~> 1.16)
111
- bundler-audit (~> 0.7.0.1)
111
+ bundler-audit (~> 0.8.0)
112
112
  dns_mock!
113
- faker (~> 2.15, >= 2.15.1)
114
- fasterer (~> 0.8.3)
113
+ faker (~> 2.17)
114
+ fasterer (~> 0.9.0)
115
115
  overcommit (~> 0.57.0)
116
116
  pry-byebug (~> 3.9)
117
117
  rake (~> 13.0, >= 13.0.3)
118
118
  reek (~> 6.0, >= 6.0.3)
119
119
  rspec (~> 3.10)
120
120
  rspec-dns (~> 0.1.8)
121
- rubocop (~> 1.8, >= 1.8.1)
122
- rubocop-performance (~> 1.9, >= 1.9.2)
123
- rubocop-rspec (~> 2.1)
121
+ rubocop (~> 1.11)
122
+ rubocop-performance (~> 1.10, >= 1.10.2)
123
+ rubocop-rspec (~> 2.2)
124
124
  simplecov (~> 0.17.1)
125
125
 
126
126
  BUNDLED WITH
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Maintainability](https://api.codeclimate.com/v1/badges/5ea9da61ef468b8ad4c4/maintainability)](https://codeclimate.com/github/mocktools/ruby-dns-mock/maintainability)
4
4
  [![Test Coverage](https://api.codeclimate.com/v1/badges/5ea9da61ef468b8ad4c4/test_coverage)](https://codeclimate.com/github/mocktools/ruby-dns-mock/test_coverage)
5
- [![CircleCI](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/develop.svg?style=svg)](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/develop)
5
+ [![CircleCI](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/master.svg?style=svg)](https://circleci.com/gh/mocktools/ruby-dns-mock/tree/master)
6
6
  [![Gem Version](https://badge.fury.io/rb/dns_mock.svg)](https://badge.fury.io/rb/dns_mock)
7
7
  [![Downloads](https://img.shields.io/gem/dt/dns_mock.svg?colorA=004d99&colorB=0073e6)](https://rubygems.org/gems/dns_mock)
8
8
  [![GitHub](https://img.shields.io/github/license/mocktools/ruby-dns-mock)](LICENSE.txt)
@@ -16,6 +16,9 @@
16
16
  - [Requirements](#requirements)
17
17
  - [Installation](#installation)
18
18
  - [Usage](#usage)
19
+ - [RSpec](#rspec)
20
+ - [DnsMock RSpec helper](#dnsmock-rspec-helper)
21
+ - [DnsMock RSpec interface](#dnsmock-rspec-interface)
19
22
  - [Contributing](#contributing)
20
23
  - [License](#license)
21
24
  - [Code of Conduct](#code-of-conduct)
@@ -23,10 +26,6 @@
23
26
  - [Versioning](#versioning)
24
27
  - [Changelog](CHANGELOG.md)
25
28
 
26
- ## Requirements
27
-
28
- Ruby MRI 2.5.0+
29
-
30
29
  ## Features
31
30
 
32
31
  - Ability to mimic any DNS records (`A`, `AAAA`, `CNAME`, `MX`, `NS`, `PTR`, `SOA` and `TXT`)
@@ -35,6 +34,10 @@ Ruby MRI 2.5.0+
35
34
  - Test framework agnostic (it's PORO, so you can use it outside of `RSpec`, `Test::Unit` or `MiniTest`)
36
35
  - Simple and intuitive DSL
37
36
 
37
+ ## Requirements
38
+
39
+ Ruby MRI 2.5.0+
40
+
38
41
  ## Installation
39
42
 
40
43
  Add this line to your application's `Gemfile`:
@@ -56,13 +59,13 @@ Or install it yourself as:
56
59
  ## Usage
57
60
 
58
61
  ```ruby
59
- # Example of mocked DNS records structure
62
+ # Example of mocked DNS records, please follow this data structure
60
63
  records = {
61
64
  'example.com' => {
62
65
  a: %w[1.1.1.1 2.2.2.2],
63
66
  aaaa: %w[2a00:1450:4001:81e::200e],
64
67
  ns: %w[ns1.domain.com ns2.domain.com],
65
- mx: %w[mx1.domain.com mx2.domain.com],
68
+ mx: %w[mx1.domain.com mx2.domain.com:50], # you can specify host(s) or host(s) with priority, use '.:0' for definition null MX record
66
69
  txt: %w[txt_record_1 txt_record_2],
67
70
  cname: 'some.domain.com',
68
71
  soa: [
@@ -77,15 +80,17 @@ records = {
77
80
  }
78
81
  ]
79
82
  },
80
- '1.1.1.1' => {
83
+ '1.2.3.4' => {
81
84
  ptr: %w[domain_1.com domain_2.com]
82
85
  }
83
86
  }
84
87
 
85
- # Main DnsMock interface. records:Hash, port:Integer are an
86
- # optional params. By default creates dns mock server with empty
87
- # records. A free port for server will be randomly assigned in
88
- # the range from 49152 to 65535. Returns current dns mock server
88
+ # Main DnsMock interface
89
+ # records:Hash, port:Integer, exception_if_not_found:Boolean
90
+ # are optional params. By default creates dns mock server with
91
+ # empty records. A free port for server will be randomly assigned
92
+ # in the range from 49152 to 65535, if record not found exception
93
+ # won't raises. Returns current dns mock server
89
94
  dns_mock_server = DnsMock.start_server(records: records) # => DnsMock::Server instance
90
95
 
91
96
  # returns current dns mock server port
@@ -108,6 +113,68 @@ DnsMock.running_servers # => [DnsMock::Server instance]
108
113
  DnsMock.stop_running_servers! # => true
109
114
  ```
110
115
 
116
+ ### RSpec
117
+
118
+ Require this either in your Gemfile or in RSpec's support scripts. So either:
119
+
120
+ ```ruby
121
+ # Gemfile
122
+ group :test do
123
+ gem 'rspec'
124
+ gem 'dns_mock', require: 'dns_mock/test_framework/rspec'
125
+ end
126
+ ```
127
+
128
+ or
129
+
130
+ ```ruby
131
+ # spec/support/config/dns_mock.rb
132
+ require 'dns_mock/test_framework/rspec'
133
+ ```
134
+
135
+ #### DnsMock RSpec helper
136
+
137
+ Just add `DnsMock::TestFramework::RSpec::Helper` if you wanna use shortcut `dns_mock_server` for DnsMock server instance into your `RSpec.describe` blocks:
138
+
139
+ ```ruby
140
+ # spec/support/config/dns_mock.rb
141
+ RSpec.configure do |config|
142
+ config.include DnsMock::TestFramework::RSpec::Helper
143
+ end
144
+ ```
145
+
146
+ ```ruby
147
+ # your awesome first_a_record_spec.rb
148
+ RSpec.describe FirstARecord do
149
+ subject(:service) do
150
+ described_class.call(
151
+ hostname,
152
+ dns_gateway_host: 'localhost',
153
+ dns_gateway_port: dns_mock_server.port
154
+ )
155
+ end
156
+
157
+ let(:hostname) { 'example.com' }
158
+ let(:first_a_record) { '1.2.3.4' }
159
+ let(:records) { { hostname => { a: [first_a_record] } } }
160
+
161
+ before { dns_mock_server.assign_mocks(records) }
162
+
163
+ it { is_expected.to eq(first_a_record) }
164
+ end
165
+ ```
166
+
167
+ #### DnsMock RSpec interface
168
+
169
+ If you won't use `DnsMock::TestFramework::RSpec::Helper` you can use `DnsMock::TestFramework::RSpec::Interface` directly instead:
170
+
171
+ ```ruby
172
+ DnsMock::TestFramework::RSpec::Interface.start_server # creates and runs DnsMock server instance
173
+ DnsMock::TestFramework::RSpec::Interface.stop_server! # stops current DnsMock server instance
174
+ DnsMock::TestFramework::RSpec::Interface.reset_mocks! # resets mocks in current DnsMock server instance
175
+ DnsMock::TestFramework::RSpec::Interface.clear_server! # stops and clears current DnsMock server instance
176
+ ```
177
+
111
178
  ## Contributing
112
179
 
113
180
  Bug reports and pull requests are welcome on GitHub at https://github.com/mocktools/ruby-dns-mock. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. Please check the [open tikets](https://github.com/mocktools/ruby-dns-mock/issues). Be shure to follow Contributor Code of Conduct below and our [Contributing Guidelines](CONTRIBUTING.md).
data/dns_mock.gemspec CHANGED
@@ -32,17 +32,17 @@ Gem::Specification.new do |spec|
32
32
  spec.require_paths = ['lib']
33
33
 
34
34
  spec.add_development_dependency 'bundler', '~> 1.16'
35
- spec.add_development_dependency 'bundler-audit', '~> 0.7.0.1'
36
- spec.add_development_dependency 'faker', '~> 2.15', '>= 2.15.1'
37
- spec.add_development_dependency 'fasterer', '~> 0.8.3'
35
+ spec.add_development_dependency 'bundler-audit', '~> 0.8.0'
36
+ spec.add_development_dependency 'faker', '~> 2.17'
37
+ spec.add_development_dependency 'fasterer', '~> 0.9.0'
38
38
  spec.add_development_dependency 'overcommit', '~> 0.57.0'
39
39
  spec.add_development_dependency 'pry-byebug', '~> 3.9'
40
40
  spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.3'
41
41
  spec.add_development_dependency 'reek', '~> 6.0', '>= 6.0.3'
42
42
  spec.add_development_dependency 'rspec', '~> 3.10'
43
43
  spec.add_development_dependency 'rspec-dns', '~> 0.1.8'
44
- spec.add_development_dependency 'rubocop', '~> 1.8', '>= 1.8.1'
45
- spec.add_development_dependency 'rubocop-performance', '~> 1.9', '>= 1.9.2'
46
- spec.add_development_dependency 'rubocop-rspec', '~> 2.1'
44
+ spec.add_development_dependency 'rubocop', '~> 1.11'
45
+ spec.add_development_dependency 'rubocop-performance', '~> 1.10', '>= 1.10.2'
46
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.2'
47
47
  spec.add_development_dependency 'simplecov', '~> 0.17.1'
48
48
  end
data/lib/dns_mock.rb CHANGED
@@ -4,8 +4,8 @@ require_relative 'dns_mock/core'
4
4
 
5
5
  module DnsMock
6
6
  class << self
7
- def start_server(server = DnsMock::Server, records: {}, port: nil)
8
- server.new(records: records, port: port)
7
+ def start_server(server = DnsMock::Server, records: {}, port: nil, exception_if_not_found: false)
8
+ server.new(records: records, port: port, exception_if_not_found: exception_if_not_found)
9
9
  end
10
10
 
11
11
  def running_servers
@@ -4,18 +4,29 @@ module DnsMock
4
4
  module Record
5
5
  module Builder
6
6
  class Mx < DnsMock::Record::Builder::Base
7
+ include DnsMock::Error::Helper
8
+
9
+ MX_RECORD_REGEX_PATTERN = /\A(.+):(\d+)|(.+)\z/.freeze
7
10
  RECORD_PREFERENCE_STEP = 10
8
11
 
9
12
  def build
10
13
  records_data.map.with_index(1) do |record_data, record_preference|
14
+ record_data, custom_record_preference = parse_mx_record_data(record_data)
11
15
  target_factory.new(
12
16
  record_data: [
13
- record_preference * DnsMock::Record::Builder::Mx::RECORD_PREFERENCE_STEP,
17
+ custom_record_preference&.to_i || record_preference * DnsMock::Record::Builder::Mx::RECORD_PREFERENCE_STEP,
14
18
  record_data
15
19
  ]
16
20
  ).create
17
21
  end
18
22
  end
23
+
24
+ private
25
+
26
+ def parse_mx_record_data(record_data)
27
+ raise_record_context_type_error(:mx, record_data, ::String)
28
+ record_data.scan(DnsMock::Record::Builder::Mx::MX_RECORD_REGEX_PATTERN).flatten.compact
29
+ end
19
30
  end
20
31
  end
21
32
  end
@@ -8,8 +8,9 @@ module DnsMock
8
8
  hash[::Resolv::DNS::Resource::IN.const_get(record_type.upcase)] = record_type
9
9
  end.freeze
10
10
 
11
- def initialize(records)
11
+ def initialize(records, exception_if_not_found)
12
12
  @records = records
13
+ @exception_if_not_found = exception_if_not_found
13
14
  end
14
15
 
15
16
  def build(hostname, record_class)
@@ -19,13 +20,13 @@ module DnsMock
19
20
 
20
21
  private
21
22
 
22
- attr_reader :records, :hostname
23
+ attr_reader :records, :exception_if_not_found, :hostname
23
24
 
24
25
  def record_by_type(record_class)
25
26
  record_type = DnsMock::Response::Answer::REVERSE_TYPE_MAPPER[record_class]
26
27
  found_records = records.dig(hostname.to_s, record_type)
27
- raise DnsMock::Error::RecordNotFound.new(record_type, hostname) unless found_records
28
- found_records
28
+ raise DnsMock::Error::RecordNotFound.new(record_type, hostname) unless found_records || !exception_if_not_found
29
+ Array(found_records)
29
30
  end
30
31
  end
31
32
  end
@@ -3,8 +3,14 @@
3
3
  module DnsMock
4
4
  module Response
5
5
  class Message
6
- def initialize(packet, records, dns_answer = DnsMock::Response::Answer, dns_message = ::Resolv::DNS::Message)
7
- @dns_answer = dns_answer.new(records)
6
+ def initialize(
7
+ packet,
8
+ records,
9
+ exception_if_not_found,
10
+ dns_answer = DnsMock::Response::Answer,
11
+ dns_message = ::Resolv::DNS::Message
12
+ )
13
+ @dns_answer = dns_answer.new(records, exception_if_not_found)
8
14
  @dns_message = dns_message.decode(packet)
9
15
  end
10
16
 
@@ -15,13 +15,15 @@ module DnsMock
15
15
  random_available_port = DnsMock::Server::RandomAvailablePort,
16
16
  thread_class = ::Thread,
17
17
  records: nil,
18
- port: nil
18
+ port: nil,
19
+ exception_if_not_found: false
19
20
  )
20
21
  @socket = socket
21
22
  @records_dictionary_builder = records_dictionary_builder
22
23
  @thread_class = thread_class
23
24
  @records = records_dictionary_builder.call(records || {})
24
25
  @port = port || random_available_port.call
26
+ @exception_if_not_found = exception_if_not_found
25
27
  prepare_server_thread
26
28
  end
27
29
 
@@ -34,7 +36,7 @@ module DnsMock
34
36
  break if packet.size.zero?
35
37
 
36
38
  address, port = addr.values_at(3, 1)
37
- socket.send(DnsMock::Response::Message.new(packet, records).as_binary_string, 0, address, port)
39
+ socket.send(DnsMock::Response::Message.new(packet, records, exception_if_not_found).as_binary_string, 0, address, port)
38
40
  end
39
41
  ensure
40
42
  socket.close
@@ -63,7 +65,7 @@ module DnsMock
63
65
 
64
66
  private
65
67
 
66
- attr_reader :socket, :records_dictionary_builder, :thread_class
68
+ attr_reader :socket, :records_dictionary_builder, :thread_class, :exception_if_not_found
67
69
  attr_accessor :records, :thread
68
70
 
69
71
  def prepare_socket_for_session
@@ -6,7 +6,8 @@ module DnsMock
6
6
  include DnsMock::Error::Helper
7
7
 
8
8
  IP_ADDRESS_PATTERN = /\A((1\d|[1-9]|2[0-4])?\d|25[0-5])(\.\g<1>){3}\z/.freeze
9
- RDNS_LOOKUP_PREFIX = '.in-addr.arpa'
9
+ IP_OCTET_GROUPS = /(\d+).(\d+).(\d+).(\d+)/.freeze
10
+ RDNS_LOOKUP_REPRESENTATION = '\4.\3.\2.\1.in-addr.arpa'
10
11
  TYPE_MAPPER = DnsMock::AVAILABLE_DNS_RECORD_TYPES.zip(
11
12
  [
12
13
  [DnsMock::Record::Builder::A, DnsMock::Record::Factory::A, ::Array],
@@ -47,7 +48,10 @@ module DnsMock
47
48
  def rdns_lookup_prefix(hostname)
48
49
  return hostname unless hostname[DnsMock::Server::RecordsDictionaryBuilder::IP_ADDRESS_PATTERN]
49
50
 
50
- "#{hostname}#{DnsMock::Server::RecordsDictionaryBuilder::RDNS_LOOKUP_PREFIX}"
51
+ hostname.gsub(
52
+ DnsMock::Server::RecordsDictionaryBuilder::IP_OCTET_GROUPS,
53
+ DnsMock::Server::RecordsDictionaryBuilder::RDNS_LOOKUP_REPRESENTATION
54
+ )
51
55
  end
52
56
 
53
57
  def build_records_instances_by_type(record_type, records_to_build)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core'
4
+ require_relative '../../dns_mock'
5
+ require_relative './rspec/interface'
6
+ require_relative './rspec/helper'
7
+
8
+ RSpec.configure do |config|
9
+ config.before(:suite) { DnsMock::TestFramework::RSpec::Interface.start_server }
10
+ config.after(:suite) { DnsMock::TestFramework::RSpec::Interface.stop_server! }
11
+ config.after { DnsMock::TestFramework::RSpec::Interface.reset_mocks! }
12
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './interface'
4
+
5
+ module DnsMock
6
+ module TestFramework
7
+ module RSpec
8
+ module Helper
9
+ def dns_mock_server(**options)
10
+ DnsMock::TestFramework::RSpec::Interface.start_server(**options)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DnsMock
4
+ module TestFramework
5
+ module RSpec
6
+ module Interface
7
+ class << self
8
+ def start_server(**options)
9
+ @dns_mock_server ||= DnsMock.start_server(**options) # rubocop:disable Naming/MemoizedInstanceVariableName
10
+ end
11
+
12
+ def stop_server!
13
+ return unless dns_mock_server
14
+
15
+ dns_mock_server.stop!
16
+ end
17
+
18
+ def reset_mocks!
19
+ return unless dns_mock_server
20
+
21
+ dns_mock_server.reset_mocks!
22
+ end
23
+
24
+ def clear_server!
25
+ @dns_mock_server = nil
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :dns_mock_server
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DnsMock
4
- VERSION = '0.2.0'
4
+ VERSION = '1.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dns_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladislav Trotsenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-25 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,48 +30,42 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.7.0.1
33
+ version: 0.8.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.7.0.1
40
+ version: 0.8.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: faker
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2.15'
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: 2.15.1
47
+ version: '2.17'
51
48
  type: :development
52
49
  prerelease: false
53
50
  version_requirements: !ruby/object:Gem::Requirement
54
51
  requirements:
55
52
  - - "~>"
56
53
  - !ruby/object:Gem::Version
57
- version: '2.15'
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: 2.15.1
54
+ version: '2.17'
61
55
  - !ruby/object:Gem::Dependency
62
56
  name: fasterer
63
57
  requirement: !ruby/object:Gem::Requirement
64
58
  requirements:
65
59
  - - "~>"
66
60
  - !ruby/object:Gem::Version
67
- version: 0.8.3
61
+ version: 0.9.0
68
62
  type: :development
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
66
  - - "~>"
73
67
  - !ruby/object:Gem::Version
74
- version: 0.8.3
68
+ version: 0.9.0
75
69
  - !ruby/object:Gem::Dependency
76
70
  name: overcommit
77
71
  requirement: !ruby/object:Gem::Requirement
@@ -174,54 +168,48 @@ dependencies:
174
168
  requirements:
175
169
  - - "~>"
176
170
  - !ruby/object:Gem::Version
177
- version: '1.8'
178
- - - ">="
179
- - !ruby/object:Gem::Version
180
- version: 1.8.1
171
+ version: '1.11'
181
172
  type: :development
182
173
  prerelease: false
183
174
  version_requirements: !ruby/object:Gem::Requirement
184
175
  requirements:
185
176
  - - "~>"
186
177
  - !ruby/object:Gem::Version
187
- version: '1.8'
188
- - - ">="
189
- - !ruby/object:Gem::Version
190
- version: 1.8.1
178
+ version: '1.11'
191
179
  - !ruby/object:Gem::Dependency
192
180
  name: rubocop-performance
193
181
  requirement: !ruby/object:Gem::Requirement
194
182
  requirements:
195
183
  - - "~>"
196
184
  - !ruby/object:Gem::Version
197
- version: '1.9'
185
+ version: '1.10'
198
186
  - - ">="
199
187
  - !ruby/object:Gem::Version
200
- version: 1.9.2
188
+ version: 1.10.2
201
189
  type: :development
202
190
  prerelease: false
203
191
  version_requirements: !ruby/object:Gem::Requirement
204
192
  requirements:
205
193
  - - "~>"
206
194
  - !ruby/object:Gem::Version
207
- version: '1.9'
195
+ version: '1.10'
208
196
  - - ">="
209
197
  - !ruby/object:Gem::Version
210
- version: 1.9.2
198
+ version: 1.10.2
211
199
  - !ruby/object:Gem::Dependency
212
200
  name: rubocop-rspec
213
201
  requirement: !ruby/object:Gem::Requirement
214
202
  requirements:
215
203
  - - "~>"
216
204
  - !ruby/object:Gem::Version
217
- version: '2.1'
205
+ version: '2.2'
218
206
  type: :development
219
207
  prerelease: false
220
208
  version_requirements: !ruby/object:Gem::Requirement
221
209
  requirements:
222
210
  - - "~>"
223
211
  - !ruby/object:Gem::Version
224
- version: '2.1'
212
+ version: '2.2'
225
213
  - !ruby/object:Gem::Dependency
226
214
  name: simplecov
227
215
  requirement: !ruby/object:Gem::Requirement
@@ -304,6 +292,9 @@ files:
304
292
  - lib/dns_mock/server.rb
305
293
  - lib/dns_mock/server/random_available_port.rb
306
294
  - lib/dns_mock/server/records_dictionary_builder.rb
295
+ - lib/dns_mock/test_framework/rspec.rb
296
+ - lib/dns_mock/test_framework/rspec/helper.rb
297
+ - lib/dns_mock/test_framework/rspec/interface.rb
307
298
  - lib/dns_mock/version.rb
308
299
  homepage: https://github.com/mocktools/ruby-dns-mock
309
300
  licenses: