mockns1

package
v2.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package mockns1 provides utilities to run a mock service that emulates the NS1 API suitible for mock testing code that relies on the gopkg.in/ns1/ns1-go.v2 pacakge

Example
package main

import (
	"net/http"
	"net/url"
	"testing"

	"github.com/stretchr/testify/require"
	"gopkg.in/ns1/ns1-go.v2/mockns1"

	api "gopkg.in/ns1/ns1-go.v2/rest"
	"gopkg.in/ns1/ns1-go.v2/rest/model/dns"
)

func main() {
	t := new(testing.T)

	// Setup the mock service
	mock, doer, err := mockns1.New(t)
	require.Nil(t, err)

	defer mock.Shutdown()

	// Create your NS1 client and configure it for the mock service
	ns1 := api.NewClient(doer, api.SetAPIKey("apikey"))
	ns1.Endpoint, _ = url.Parse("https://" + mock.Address + "/v1/")

	// Add your test case (zone list in this example)
	require.Nil(t, mock.AddTestCase(http.MethodGet, "zones", http.StatusOK, nil, nil, "",
		[]*dns.Zone{{Zone: "foo.bar"}}))

	// Perform your tests
	zones, _, err := ns1.Zones.List()
	require.Nil(t, err)
	require.Equal(t, 1, len(zones))
	require.Equal(t, "foo.bar", zones[0].Zone)

}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	// Address is set by New() to the listen address of the mock server
	Address string
	// contains filtered or unexported fields
}

Service is a controller for a mock server suitable for responding to gopkg.in/ns1/ns1-go.v2 client requests. This object should always be initialized via the New() function.

func New

func New(tb testing.TB) (*Service, api.Doer, error)

New creates and starts a new TLS based *httptest.Server instance. As a self signed certificate is being used by the server, your HTTP client being used with your NS1 client needs to disable TLS verification. The returned Doer is a *http.Client with TLS verification disable that is appropriate for use.

The mock service is compatible with both testing.B and testing.T instances so that it may be used in benchmarking as well as testing. If a testing.B instance is supplied the timer will be stopped for the duration of the ServeHTTP() and AddTestCase() methods to minimise the impact on your benchmark statistics.

func (*Service) AddActivateVersionTestCase added in v2.7.7

func (s *Service) AddActivateVersionTestCase(
	zoneName string,
	versionID int64,
	requestHeaders, responseHeaders http.Header,
) error

AddActivateVersionTestCase sets up a test case for the api.Client.Versions.Activate() function

func (*Service) AddActivityListTestCase added in v2.10.0

func (s *Service) AddActivityListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*account.Activity,
	params ...api.Param,
) error

AddActivityListTestCase sets up a test case for the api.Client.Activity.List() function

func (*Service) AddApplicationCreateTestCase

func (s *Service) AddApplicationCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	application, response *pulsar.Application,
) error

AddApplicationCreateTestCase sets up a test case for the api.Client.application.Create() function

func (*Service) AddApplicationDeleteTestCase

func (s *Service) AddApplicationDeleteTestCase(
	id string, requestHeaders, responseHeaders http.Header,
) error

AddApplicationDeleteTestCase sets up a test case for the api.Client.application.Delete() function

func (*Service) AddApplicationGetTestCase

func (s *Service) AddApplicationGetTestCase(
	id string,
	requestHeaders, responseHeaders http.Header,
	response *pulsar.Application,
) error

AddApplicationGetTestCase sets up a test case for the api.Client.application.Get() function

func (*Service) AddApplicationTestCase

func (s *Service) AddApplicationTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*pulsar.Application,
) error

AddApplicationTestCase sets up a test case for the api.Client.application.List() function

func (*Service) AddApplicationUpdateTestCase

func (s *Service) AddApplicationUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	application, response *pulsar.Application,
) error

AddApplicationUpdateTestCase sets up a test case for the api.Client.application.Update() function

func (*Service) AddCreateVersionTestCase added in v2.7.7

func (s *Service) AddCreateVersionTestCase(
	zoneName string,
	requestHeaders, responseHeaders http.Header,
	response *dns.Version,
) error

AddCreateVersionTestCase sets up a test case for the api.Client.Versions.Create() function

func (*Service) AddDNSViewCreateTestCase

func (s *Service) AddDNSViewCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	dnsView, response *dns.View,
) error

AddDNSViewCreateTestCase sets up a test case for the api.Client.View.Create() function

func (*Service) AddDNSViewGetPreferencesTestCase

func (s *Service) AddDNSViewGetPreferencesTestCase(
	requestHeaders, responseHeaders http.Header,
	response interface{},
) error

AddDNSViewGetPreferencesTestCase sets up a test case for the api.Client.View.GetPreferences() function

func (*Service) AddDNSViewGetTestCase

func (s *Service) AddDNSViewGetTestCase(
	viewName string,
	requestHeaders, responseHeaders http.Header,
	response *dns.View,
) error

AddDNSViewGetTestCase sets up a test case for the api.Client.View.Get() function

func (*Service) AddDNSViewListTestCase

func (s *Service) AddDNSViewListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*dns.View,
) error

AddDNSViewListTestCase sets up a test case for the api.Client.View.List() function

func (*Service) AddDNSViewUpdatePreferencesTestCase

func (s *Service) AddDNSViewUpdatePreferencesTestCase(
	requestHeaders, responseHeaders http.Header,
	body, response interface{},
) error

AddDNSViewUpdatePreferencesTestCase sets up a test case for the api.Client.View.GetPreferences() function

func (*Service) AddDNSViewUpdateTestCase

func (s *Service) AddDNSViewUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	dnsView, response *dns.View,
) error

AddDNSViewUpdateTestCase sets up a test case for the api.Client.View.Update() function

func (*Service) AddDatasetCreateTestCase added in v2.8.0

func (s *Service) AddDatasetCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	request, response *dataset.Dataset,
) error

AddDatasetCreateTestCase sets up a test case for the api.Client.Datasets.Create() function

func (*Service) AddDatasetDeleteTestCase added in v2.8.0

func (s *Service) AddDatasetDeleteTestCase(
	id string, requestHeaders, responseHeaders http.Header,
) error

AddDatasetDeleteTestCase sets up a test case for the api.Client.Datasets.Delete() function

func (*Service) AddDatasetGetReportTestCase added in v2.8.0

func (s *Service) AddDatasetGetReportTestCase(
	id string, reportId string, requestHeaders, responseHeaders http.Header, fileContents []byte,
) error

AddDatasetGetReportTestCase sets up a test case for the api.Client.Datasets.GetReport() function

func (*Service) AddDatasetGetTestCase added in v2.8.0

func (s *Service) AddDatasetGetTestCase(
	id string,
	requestHeaders, responseHeaders http.Header,
	response *dataset.Dataset,
) error

AddDatasetGetTestCase sets up a test case for the api.Client.Datasets.Get() function

func (*Service) AddDatasetListTestCase added in v2.8.0

func (s *Service) AddDatasetListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*dataset.Dataset,
) error

AddDatasetListTestCase sets up a test case for the api.Client.Datasets.List() function

func (*Service) AddDeleteVersionTestCase added in v2.7.7

func (s *Service) AddDeleteVersionTestCase(
	zoneName string,
	versionID int64,
	requestHeaders, responseHeaders http.Header,
) error

AddDeleteVersionTestCase sets up a test case for the api.Client.Versions.Delete() function

func (*Service) AddGlobalIPWhitelistCreateTestCase added in v2.7.9

func (s *Service) AddGlobalIPWhitelistCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	whitelist, response *account.IPWhitelist,
) error

AddGlobalIPWhitelistCreateTestCase sets up a test case for the api.Client.GlobalIPWhitelist.Create() function

func (*Service) AddGlobalIPWhitelistDeleteTestCase added in v2.7.9

func (s *Service) AddGlobalIPWhitelistDeleteTestCase(
	whitelistID string,
	requestHeaders, responseHeaders http.Header,
) error

AddGlobalIPWhitelistDeleteTestCase sets up a test case for the api.Client.GlobalIPWhitelist.Delete() function

func (*Service) AddGlobalIPWhitelistGetTestCase added in v2.7.9

func (s *Service) AddGlobalIPWhitelistGetTestCase(
	whitelistID string,
	requestHeaders, responseHeaders http.Header,
	response *account.IPWhitelist,
) error

AddGlobalIPWhitelistGetTestCase sets up a test case for the api.Client.GlobalIPWhitelist.Get() function

func (*Service) AddGlobalIPWhitelistListTestCase added in v2.7.9

func (s *Service) AddGlobalIPWhitelistListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*account.IPWhitelist,
) error

AddGlobalIPWhitelistListTestCase sets up a test case for the api.Client.GlobalIPWhitelist.List() function

func (*Service) AddGlobalIPWhitelistUpdateTestCase added in v2.7.9

func (s *Service) AddGlobalIPWhitelistUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	whitelist, response *account.IPWhitelist,
) error

AddGlobalIPWhitelistUpdateTestCase sets up a test case for the api.Client.GlobalIPWhitelist.Update() function

func (*Service) AddMonitorRegionsListTestCase added in v2.9.0

func (s *Service) AddMonitorRegionsListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*monitor.Region,
) error

AddMonitorRegionsListTestCase sets up a test case for the api.Client.MonitorRegionsService.List() function.

func (*Service) AddPulsarJobCreateTestCase

func (s *Service) AddPulsarJobCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	pulsarJob, response *pulsar.Job,
) error

AddPulsarJobCreateTestCase sets up a test case for the api.Client.PulsarJobs.Create() function

func (*Service) AddPulsarJobDeleteTestCase

func (s *Service) AddPulsarJobDeleteTestCase(
	requestHeaders, responseHeaders http.Header,
	pulsarJob, response *pulsar.Job,
) error

AddPulsarJobDeleteTestCase sets up a test case for the api.Client.PulsarJobs.Delete() function

func (*Service) AddPulsarJobGetTestCase

func (s *Service) AddPulsarJobGetTestCase(
	appid, jobid string,
	requestHeaders, responseHeaders http.Header,
	response *pulsar.Job,
) error

AddPulsarJobGetTestCase sets up a test case for the api.Client.PulsarJobs.Get() function

func (*Service) AddPulsarJobListTestCase

func (s *Service) AddPulsarJobListTestCase(
	appid string,
	requestHeaders, responseHeaders http.Header,
	response []*pulsar.Job,
) error

AddPulsarJobListTestCase sets up a test case for the api.Client.PulsarJobs.List() function

func (*Service) AddPulsarJobUpdateTestCase

func (s *Service) AddPulsarJobUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	pulsarJob, response *pulsar.Job,
) error

AddPulsarJobUpdateTestCase sets up a test case for the api.Client.PulsarJobs.Update() function

func (*Service) AddTestCase

func (s *Service) AddTestCase(
	method, uri string, returnStatus int,
	requestHeaders, responseHeaders http.Header,
	requestBody, responseBody interface{},
	params ...api.Param,
) error

AddTestCase adds a new test case to the mock service. Test cases are unique based on the method, uri, request headers, and request body.

func (*Service) AddTsigKeyCreateTestCase

func (s *Service) AddTsigKeyCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	tsigKey, response *dns.TSIGKey,
) error

AddTsigKeyCreateTestCase sets up a test case for the api.Client.TSIG.Create() function

func (*Service) AddTsigKeyDeleteTestCase

func (s *Service) AddTsigKeyDeleteTestCase(
	requestHeaders, responseHeaders http.Header,
	tsigKey, response *dns.TSIGKey,
) error

AddTsigKeyDeleteTestCase sets up a test case for the api.Client.TSIG.Delete() function

func (*Service) AddTsigKeyGetTestCase

func (s *Service) AddTsigKeyGetTestCase(
	name string,
	requestHeaders, responseHeaders http.Header,
	response *dns.TSIGKey,
) error

AddTsigKeyGetTestCase sets up a test case for the api.Client.TSIG.Get() function

func (*Service) AddTsigKeyListTestCase

func (s *Service) AddTsigKeyListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*dns.TSIGKey,
) error

AddTsigKeyListTestCase sets up a test case for the api.Client.TSIG.List() function

func (*Service) AddTsigKeyUpdateTestCase

func (s *Service) AddTsigKeyUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	tsigKey, response *dns.TSIGKey,
) error

AddTsigKeyUpdateTestCase sets up a test case for the api.Client.TSIG.Update() function

func (*Service) AddVersionListTestCase added in v2.7.7

func (s *Service) AddVersionListTestCase(
	zoneName string,
	requestHeaders, responseHeaders http.Header,
	response []*dns.Version,
) error

AddZoneListTestCase sets up a test case for the api.Client.Versions.List() function

func (*Service) AddZoneCreateTestCase

func (s *Service) AddZoneCreateTestCase(
	requestHeaders, responseHeaders http.Header,
	zone, response *dns.Zone,
) error

AddZoneCreateTestCase sets up a test case for the api.Client.Zones.Create() function

func (*Service) AddZoneDeleteTestCase

func (s *Service) AddZoneDeleteTestCase(
	name string, requestHeaders, responseHeaders http.Header,
) error

AddZoneDeleteTestCase sets up a test case for the api.Client.Zones.Delete() function

func (*Service) AddZoneGetTestCase

func (s *Service) AddZoneGetTestCase(
	name string,
	requestHeaders, responseHeaders http.Header,
	response *dns.Zone,
	records bool,
) error

AddZoneGetTestCase sets up a test case for the api.Client.Zones.Get() function

func (*Service) AddZoneListTestCase

func (s *Service) AddZoneListTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*dns.Zone,
) error

AddZoneListTestCase sets up a test case for the api.Client.Zones.List() function

func (*Service) AddZoneUpdateTestCase

func (s *Service) AddZoneUpdateTestCase(
	requestHeaders, responseHeaders http.Header,
	zone, response *dns.Zone,
) error

AddZoneUpdateTestCase sets up a test case for the api.Client.Zones.Update() function

func (*Service) ClearTestCases

func (s *Service) ClearTestCases()

ClearTestCases removes all previously added test cases

func (*Service) NetworkGetTestCase added in v2.7.4

func (s *Service) NetworkGetTestCase(
	requestHeaders, responseHeaders http.Header,
	response []*dns.Network,
) error

NetworkGetTestCase sets up a test case for the api.Client.Network.Get() function

func (*Service) ServeHTTP

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP is the request handler for the mock service. This should not be called directly in unit tests.

func (*Service) Shutdown

func (s *Service) Shutdown()

Shutdown cleans up the mock instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL