apns2mock

package
v0.0.0-...-1988585 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2017 License: MIT Imports: 18 Imported by: 1

Documentation

Overview

Package apns2mock provides a way of creating emulators of Apple Push Notification HTTP/2 service. Emulators can be integrated in automated testing code or can be used to provide standalone mock services.

Index

Constants

View Source
const AutoCert = ""

AutoCert can be supplied to NewServer as certFile argument instead of "" for improved semantics indicating that server certificate and key should be auto-generated.

View Source
const AutoKey = ""

AutoKey can be supplied to NewServer as keyFile argument instead of "" for improved semantics indicating that server certificate and key should be auto-generated.

View Source
const RequestRoot = "/3/device/"

APNS default root URL path.

Variables

View Source
var AllOkayHandler = allOkayHandler{}

AllOkayHandler always respons with status 200 and a valid APN ID.

View Source
var AuthTokenHandlers []HadlerFunc

AuthTokenHandlers deal with provider JWT tokens.

Expired tokens are 403, "ExpiredProviderToken".

Tokens with incorrct signing algorithm are 403, "InvalidProviderToken".

Team ID (JWT "iss" claim) starting with '1' are 403, "InvalidProviderToken".

View Source
var DeviceTokenHandlers []HadlerFunc

DeviceTokenHandlers deal with device tokens:

Device tokens starting with '1' are 400, "BadDeviceToken".

Device tokens starting with '2' are 410, "Unregistered".

Device tokens starting with the same letter/digit as APNS topic are 400, "DeviceTokenNotForTopic".

View Source
var HeaderHandlers []HadlerFunc

HeaderHandlers deal with request headers:

Invalid UUID format in APNS Id is 400, "BadMessageId".

Priority that is not empty, 5 or 10 is 400, "BadPriority".

Empty topic is 400, "MissingTopic".

Topics starting with 'd' are 400, "TopicDisallowed".

Collapse Ids longer than 64 are 400, "BadCollapseId".

Expiration date that cannot be parsed is 400, "BadExpirationDate".

View Source
var NoDelayCommsCfg = CommsCfg{
	MaxConcurrentStreams: 500,
	MaxConns:             1000,
	ConnectionDelay:      0,
	ResponseTime:         0,
}

NoDelayCommsCfg contains settings that do not introduce any additional delay in the mock server handling of the requests and incoming client connections.

View Source
var TypicalCommsCfg = CommsCfg{
	MaxConcurrentStreams: 500,
	MaxConns:             1000,
	ConnectionDelay:      1 * time.Second,
	ResponseTime:         20 * time.Millisecond,
}

TypicalCommsCfg contains settings that emulate typical latency and connection handling behavior of actual APNS servers.

Functions

This section is empty.

Types

type APNSRequest

type APNSRequest struct {

	// DeviceToken is device token from the original request path.
	DeviceToken string

	// Header is http.Header of the original request.
	Header http.Header

	// TokenHeader is parsed headers of JWT provider token.
	TokenHeader map[string]interface{}

	// TokenClaims is parsed claims of JWT provider token.
	TokenClaims map[string]interface{}

	// Payload is parsed request payload.
	Payload map[string]interface{}
}

APNSRequest represents parsed and validated request to APN service.

type CaseHandler

type CaseHandler struct {
	// CaseHandlers are asked to evaluate APNSRequest in order of
	// their appearance. The first case handler returning non-zero status code
	// is selected and its rejection reason is sent in the response.
	//
	// If none of the case handlers return non-zero status code, as 200
	// successful response is sent back to the client.
	CaseHandlers []HadlerFunc
}

CaseHandler handles all requests rooted at /3/device/ path. It validates all request attributes, including method, required headers, request path and payload body. JSON encoded values are unmashalled and related errors are handled. If all of the above checks are successful, parsed APNSRequest is evaluated by hander's case handlers.

You can choose from a set of predefined case handlers or define some of your own. Case handler slices can be joined together to form larger combined lists.

var CertAuthHandler *CaseHandler

CertAuthHandler only handles client certificate-based requests.

var DefaultHandler *CaseHandler

DefaultHandler handles provider token-based and client certificate-based requests.

var TokenAuthHandler *CaseHandler

TokenHandler only handles provider token-based requests.

func (*CaseHandler) ServeHTTP

func (h *CaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves all incoming HTTP requests. It performs initial request validation and parsing before delegating to CaseHandlers.

type CommsCfg

type CommsCfg struct {

	// MaxConcurrentStreams is the maximum allowed number of concurrent streams
	// per HTTP/2 connection. This will be communicated to clients in HTTP/2
	// SETTINGS frame.
	MaxConcurrentStreams uint32

	// MaxConns is maximum allowed number of concurrent connections. Attempted
	// connections over this limit will be dropped by the server.
	MaxConns uint32

	// ConnectionDelay delays connection accept by the specified time.
	ConnectionDelay time.Duration

	// ResponseTime is the time to be taken to respond to a request other than
	// 404 BadPath response.
	ResponseTime time.Duration
}

CommsCfg can be used to configure communications aspects of the mock server.

type HadlerFunc

type HadlerFunc func(req *APNSRequest) (statusCode int, rejectionReason string)

func JoinHandlers

func JoinHandlers(hs ...[]HadlerFunc) []HadlerFunc

JoinHandlers is a convenience function that joins all supplied handlers and returns the combine slice.

type Server

type Server struct {

	// We are wrapping httptest.Server in order to extend functionality
	// and to backfill features missing in go 1.7.
	*httptest.Server

	// RootCertificate to use when setting up client TLS's RootCAs.
	RootCertificate *tls.Certificate
	// contains filtered or unexported fields
}

Server represents a mock APNS service. See NewServer for information on creating servers.

func NewServer

func NewServer(commsCfg CommsCfg, handler http.Handler, certFile string, keyFile string) (*Server, error)

NewServer creates and starts a new Server instance with handler servicing requests on RequestRoot and with all other paths returning 404 status. If certFile and keyFile are not empty, the server TLS certicicate will be loaded from the specified files.

If either certFile or keyFile is empty a new self-signed certificate will be created. This is usually all that is needed for integrating mock server in automated tests. Simply use server's pre-configured client for your testing or retrieve server's URL and root certificate to configure your custom client.

func (*Server) BecomeAvailable

func (s *Server) BecomeAvailable()

BecomeAvailable restores normal request handling flow.

func (*Server) BecomeUnavailable

func (s *Server) BecomeUnavailable(statusCode int, reason string)

BecomeUnavailable makes server begin responding with specified status code and reason to any future requests. This is typically used to test handling of 5XX status codes by clients.

func (*Server) Client

func (s *Server) Client() *http.Client

Client returns an HTTP client configured for making requests to the server. It is configured to trust the server's TLS test certificate and will close its idle connections on Server.Close.

Jump to

Keyboard shortcuts

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