All Projects → mocktools → ruby-dns-mock

mocktools / ruby-dns-mock

Licence: MIT license
DNS mock server written on 💎 Ruby. Mimic any DNS records for your test environment with fake DNS server.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to ruby-dns-mock

go-smtp-mock
SMTP mock server written on Golang. Mimic any 📤 SMTP server behavior for your test environment with fake SMTP server.
Stars: ✭ 76 (+52%)
Mutual labels:  mock-server, testing-tools, fake-server, mocktools
json-fake-server
Simple way to create http server (node js) https://www.npmjs.com/package/test-fake-server
Stars: ✭ 15 (-70%)
Mutual labels:  mock, mock-server, fake-server
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-46%)
Mutual labels:  mock, mock-server, testing-tools
main
Mocks Server monorepo
Stars: ✭ 109 (+118%)
Mutual labels:  mock, mock-server, testing-tools
Superagent Mocker
Pretty simple in-browser mocks for CRUD and REST API
Stars: ✭ 127 (+154%)
Mutual labels:  mock, testing-tools
Ava Playback
📼 🚀 Record and playback http requests from your ava tests
Stars: ✭ 124 (+148%)
Mutual labels:  mock, testing-tools
platypus
Very simple and customizable mock/echo server
Stars: ✭ 15 (-70%)
Mutual labels:  mock, mock-server
Mockinizer
An okhttp / retrofit api call mocking library
Stars: ✭ 176 (+252%)
Mutual labels:  mock, mock-server
Httpmock
HTTP mocking library for Rust.
Stars: ✭ 76 (+52%)
Mutual labels:  mock, mock-server
Httpretty
Intercept HTTP requests at the Python socket level. Fakes the whole socket module
Stars: ✭ 1,930 (+3760%)
Mutual labels:  mock, testing-tools
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+258%)
Mutual labels:  mock, testing-tools
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+3192%)
Mutual labels:  mock, testing-tools
Prig
Prig is a lightweight framework for test indirections in .NET Framework.
Stars: ✭ 106 (+112%)
Mutual labels:  mock, testing-tools
Weld
Full fake REST API generator written with Rust
Stars: ✭ 146 (+192%)
Mutual labels:  mock, mock-server
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+2652%)
Mutual labels:  mock, testing-tools
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+24806%)
Mutual labels:  mock, testing-tools
Charlatan
Go Interface Mocking Tool
Stars: ✭ 195 (+290%)
Mutual labels:  mock, testing-tools
Openapi Mock
OpenAPI mock server with random data generation
Stars: ✭ 202 (+304%)
Mutual labels:  mock, mock-server
Webmockr
R library for stubbing and setting expectations on HTTP requests
Stars: ✭ 37 (-26%)
Mutual labels:  mock, testing-tools
Wiremockui
Wiremock UI - Tool for creating mock servers, proxies servers and proxies servers with the option to save the data traffic from an existing API or Site.
Stars: ✭ 38 (-24%)
Mutual labels:  mock, mock-server

Ruby DnsMock - mimic any DNS records for your test environment and even more!

Maintainability Test Coverage CircleCI Gem Version Downloads In Awesome Ruby GitHub Contributor Covenant

💎 Ruby DNS mock. Mimic any DNS records for your test environment and even more.

Table of Contents

Features

  • Ability to mimic any DNS records (A, AAAA, CNAME, MX, NS, PTR, SOA and TXT)
  • Automatically converts host names to RDNS/Punycode representation
  • Lightweight UDP DNS mock server with dynamic/manual port assignment
  • Test framework agnostic (it's PORO, so you can use it outside of RSpec, Test::Unit or MiniTest)
  • Simple and intuitive DSL
  • Only one runtime dependency

Requirements

Ruby MRI 2.5.0+

Installation

Add this line to your application's Gemfile:

group :development, :test do
  gem 'dns_mock', require: false
end

And then execute:

bundle

Or install it yourself as:

gem install dns_mock

Usage

# Example of mocked DNS records, please follow this data structure
records = {
  'example.com' => {
    a: %w[1.1.1.1 2.2.2.2],
    aaaa: %w[2a00:1450:4001:81e::200e],
    ns: %w[ns1.domain.com ns2.domain.com],
    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
    txt: %w[txt_record_1 txt_record_2],
    cname: 'mañana.com', # you can specify hostname in UTF-8. It will be converted to xn--maana-pta.com automatically
    soa: [
      {
        mname: 'dns1.domain.com',
        rname: 'dns2.domain.com',
        serial: 2_035_971_683,
        refresh: 10_000,
        retry: 2_400,
        expire: 604_800,
        minimum: 3_600
      }
    ]
  },
  '1.2.3.4' => { # You can define RDNS host address without lookup prefix. It will be converted to 4.3.2.1.in-addr.arpa automatically
    ptr: %w[domain_1.com domain_2.com]
  }
}

# Public DnsMock interface
# records:Hash, port:Integer, exception_if_not_found:Boolean
# are optional params. By default creates dns mock server with
# empty records. A free port for server will be randomly assigned
# in the range from 49152 to 65535, if record not found exception
# won't raises. Please note if you specify zero port number,
# free port number will be randomly assigned as a server port too.
# Returns current dns mock server
dns_mock_server = DnsMock.start_server(records: records) # => DnsMock::Server instance

# returns current dns mock server port
dns_mock_server.port # => 49322

# interface to setup mock records.
# Available only in case when server mocked records is empty
dns_mock_server.assign_mocks(records) # => true/nil

# interface to reset current mocked records
dns_mock_server.reset_mocks! # => true

# interface to stop current dns mock server
dns_mock_server.stop! # => true

# returns list of running dns mock servers
DnsMock.running_servers # => [DnsMock::Server instance]

# interface to stop all running dns mock servers
DnsMock.stop_running_servers! # => true

RSpec

Require this either in your Gemfile or in RSpec's support scripts. So either:

# Gemfile
group :test do
  gem 'rspec'
  gem 'dns_mock', require: 'dns_mock/test_framework/rspec'
end

or

# spec/support/config/dns_mock.rb
require 'dns_mock/test_framework/rspec'

DnsMock RSpec helper

Just add DnsMock::TestFramework::RSpec::Helper if you wanna use shortcut dns_mock_server for DnsMock server instance inside of your RSpec.describe blocks:

# spec/support/config/dns_mock.rb
RSpec.configure do |config|
  config.include DnsMock::TestFramework::RSpec::Helper
end
# your awesome first_a_record_spec.rb
RSpec.describe FirstARecord do
  subject(:service) do
    described_class.call(
      hostname,
      dns_gateway_host: 'localhost',
      dns_gateway_port: dns_mock_server.port
    )
  end

  let(:hostname) { 'example.com' }
  let(:first_a_record) { '1.2.3.4' }
  let(:records) { { hostname => { a: [first_a_record] } } }

  before { dns_mock_server.assign_mocks(records) }

  it { is_expected.to eq(first_a_record) }
end

DnsMock RSpec interface

If you won't use DnsMock::TestFramework::RSpec::Helper you can use DnsMock::TestFramework::RSpec::Interface directly instead:

DnsMock::TestFramework::RSpec::Interface.start_server  # creates and runs DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.stop_server!  # stops current DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.reset_mocks!  # resets mocks in current DnsMock server instance
DnsMock::TestFramework::RSpec::Interface.clear_server! # stops and clears current DnsMock server instance

Contributing

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 code of conduct. Please check the open tickets. Be sure to follow Contributor Code of Conduct below and our Contributing Guidelines.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the DnsMock project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Credits

Versioning

DnsMock uses Semantic Versioning 2.0.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].