common

package
v3.8.24 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package common provides shared test utilities and mocks for unit testing

Package common provides test utilities for unit tests that actually exercise SDK code

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMockHTTPResponse

func CreateMockHTTPResponse(statusCode int, body string) *http.Response

CreateMockHTTPResponse creates an http.Response for testing

func CreateMockHTTPResponseWithHeaders

func CreateMockHTTPResponseWithHeaders(statusCode int, body string, headers map[string]string) *http.Response

CreateMockHTTPResponseWithHeaders creates an http.Response with custom headers

func CreateTestService

func CreateTestService(ctx context.Context, server *TestServer, customerID string) (*zscaler.Service, error)

CreateTestService creates a zscaler.Service configured to use the mock server This allows tests to call actual SDK functions and generate real coverage

func CreateZWATestService

func CreateZWATestService(ctx context.Context, server *TestServer) (*zwaservices.Service, error)

CreateZWATestService creates a ZWA service configured to use the mock server

func MockSegmentGroupListResponse

func MockSegmentGroupListResponse(groups ...map[string]interface{}) map[string]interface{}

MockSegmentGroupListResponse returns a mock list of segment groups

func MockSegmentGroupResponse

func MockSegmentGroupResponse(id, name string) map[string]interface{}

MockSegmentGroupResponse returns a mock segment group response

func MustCreateTestService

func MustCreateTestService(server *TestServer, customerID string) *zscaler.Service

MustCreateTestService creates a test service or panics

Types

type MockHandler

type MockHandler struct {
	Responses map[string]map[string]MockResponse // path -> method -> response
	Requests  []RecordedRequest
	// CallCount tracks how many times each path/method combination was called
	CallCount map[string]int
	// contains filtered or unexported fields
}

MockHandler is a configurable HTTP handler for testing

func NewMockHandler

func NewMockHandler() *MockHandler

NewMockHandler creates a new mock handler

func (*MockHandler) On

func (m *MockHandler) On(method, path string, response MockResponse) *MockHandler

On registers a mock response for a path and method

func (*MockHandler) ServeHTTP

func (m *MockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler

type MockResponse

type MockResponse struct {
	StatusCode int
	Body       interface{}
	Headers    map[string]string
	Delay      time.Duration
}

MockResponse represents a configured mock response

func CSVResponse

func CSVResponse(csvData string) MockResponse

CSVResponse creates a CSV response

func ConflictResponse

func ConflictResponse(body string) MockResponse

ConflictResponse creates a 409 Conflict response

func CreatedResponse

func CreatedResponse(body interface{}) MockResponse

CreatedResponse creates a 201 Created response

func EditLockResponse

func EditLockResponse() MockResponse

EditLockResponse creates a 409 EDIT_LOCK_NOT_AVAILABLE response

func MockOAuthResponse

func MockOAuthResponse() MockResponse

MockOAuthResponse returns a valid OAuth token response

func MockZPAOAuthResponse

func MockZPAOAuthResponse() MockResponse

MockZPAOAuthResponse returns a valid ZPA OAuth token response

func NoContentResponse

func NoContentResponse() MockResponse

NoContentResponse creates a 204 No Content response

func NotFoundResponse

func NotFoundResponse() MockResponse

NotFoundResponse creates a 404 Not Found response

func RawResponse

func RawResponse(body []byte, statusCode int, headers map[string]string) MockResponse

RawResponse creates a response with raw bytes and custom content type

func ResourceAccessBlockedResponse

func ResourceAccessBlockedResponse() MockResponse

ResourceAccessBlockedResponse creates a 401 Resource Access Blocked response

func ServerErrorResponse

func ServerErrorResponse() MockResponse

ServerErrorResponse creates a 500 Internal Server Error response

func SessionInvalidResponse

func SessionInvalidResponse() MockResponse

SessionInvalidResponse creates a 401 SESSION_NOT_VALID response

func SuccessResponse

func SuccessResponse(body interface{}) MockResponse

SuccessResponse creates a 200 OK response

func SuccessResponseWithStatus

func SuccessResponseWithStatus(statusCode int, body interface{}) MockResponse

SuccessResponseWithStatus creates a response with a custom status code

func TooManyRequestsResponse

func TooManyRequestsResponse(retryAfterSeconds int) MockResponse

TooManyRequestsResponse creates a 429 Too Many Requests response

func TooManyRequestsResponseWithHeader

func TooManyRequestsResponseWithHeader(retryAfter string) MockResponse

TooManyRequestsResponseWithHeader creates a 429 response with custom Retry-After

func UnauthorizedResponse

func UnauthorizedResponse(body string) MockResponse

UnauthorizedResponse creates a 401 Unauthorized response

type MockTransport

type MockTransport struct {
	TestServerURL string
}

MockTransport redirects all HTTP requests to a test server

func (*MockTransport) RoundTrip

func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error)

type RecordedRequest

type RecordedRequest struct {
	Method  string
	Path    string
	Query   string
	Headers http.Header
	Body    []byte
}

RecordedRequest stores information about a received request

type TestServer

type TestServer struct {
	*httptest.Server
	Handler *MockHandler
}

TestServer wraps httptest.Server with additional utilities

func NewTestServer

func NewTestServer() *TestServer

NewTestServer creates a new test server with the mock handler

func (*TestServer) AssertRequestCount

func (ts *TestServer) AssertRequestCount(t *testing.T, expected int)

AssertRequestCount verifies the number of requests made

func (*TestServer) GetCallCount

func (ts *TestServer) GetCallCount(method, path string) int

GetCallCount returns the number of times a specific path/method was called

func (*TestServer) LastRequest

func (ts *TestServer) LastRequest() *RecordedRequest

LastRequest returns the most recent request

func (*TestServer) On

func (ts *TestServer) On(method, path string, response MockResponse) *TestServer

On is a convenience method to add mock responses

Jump to

Keyboard shortcuts

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