mock

package module
v0.0.0-...-4e3be49 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2016 License: MIT Imports: 15 Imported by: 0

README

#GetEventStore Atom Feed Mock

The Atom feed handler provides an easy way to provide a mock atom feed when testing code that reads Atom feeds from GetEventStore.

While I was developing the Go.GetEventStore client for the GetEventStore HTTP API, I found that there was a lot of overhead in setting up mock data for tests. Creating Atom feeds as string literals was just too fiddly.

Using the simulator you can set up tests running against realistic data in just a few lines of code.

The package also provides a number of fuctions for creating test events and metadata.

###Get the package


    $ go get github.com/jetbasrawi/go.geteventstore.testfeed

While unit testing, the handler can be used with the test server in the "net/http/httptest" package


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

    "github.com/jetbasrawi/go.geteventstore.testfeed"
)

var (

	// mux is the HTTP request multiplexer used with the test server
	mux *http.ServeMux

	// server is a test HTTP server used to provide mock API responses
	server *httptest.Server

)

func setup() {
    // Initialize multiplexer
	mux = http.NewServeMux()

    // Initialize test server
	server = httptest.NewServer(mux)

    // Create 50 test events to be served by the mock feed handler
    // The events will be of the types specified in the variadic eventType argument
    es := mock.CreateTestEvents(50, "foostream", server.URL, "FooEventType", "BarEventType")

    // Create a new mock feed handler
    handler, err := mock.NewAtomFeedSimulator(es, u, m, -1)
	if err != nil {
		log.Fatal(err)
	}

    // Add the handler to the multiplexer
	mux.Handle("/", handler)

}

func teardown() {
    // Close the server
	server.Close()
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTestFeed

func CreateTestFeed(es []*Event, feedURL string) (*atom.Feed, error)

CreateTestFeed creates an atom feed object from the events passed in and the url provided.

The function will take the events that correspond to the version number and page size in the url and return a feed object that contains those events. If the url defines a set larger than the events passed in the returned events will only contain the events available.

Types

type AtomFeedSimulator

type AtomFeedSimulator struct {
	sync.Mutex
	Events   []*Event
	BaseURL  *url.URL
	MetaData *Event

	TrickleAfter int
	// contains filtered or unexported fields
}

AtomFeedSimulator is the type that stores configuration and state for the feed simulator.

func NewAtomFeedSimulator

func NewAtomFeedSimulator(events []*Event, baseURL *url.URL, streamMeta *Event, trickleAfter int) (*AtomFeedSimulator, error)

NewAtomFeedSimulator consructs a new AtomFeedSimulator.

events is a slice of *Event that will be returned by the handler. The events are equivalent to the total number of events in a stream and these can be read and paged as you would read and page a stream in GetEventStore. The number of events must be greater than 0.

baseURL is the base url of the test server.

streamMeta is the stream metadata that should be returned if a request for metadata is made to the server.

trickleAfter is used to simulate polling and the arrival of new events while polling. The simulator will return any events after the version specified by the trickleAfter argument. For example, if ten events are passed in and trickleAfter is set to 5, the first five events will be returned in a feed page as if they existed before the request and then a subsequent poll to the head of the stream will return no events. Set the LongPoll header and the simulator will return the next five events at some random interval between 0 seconds and the number of seconds specified by the value of the LongPoll header. If you want all events to be returned as existing, set trickleAfter to -1

func (*AtomFeedSimulator) ServeHTTP

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

ServeHTTP serves atom feed responses

type Event

type Event struct {
	EventStreamID string      `json:"eventStreamId,omitempty"`
	EventNumber   int         `json:"eventNumber,omitempty"`
	EventType     string      `json:"eventType,omitempty"`
	EventID       string      `json:"eventId,omitempty"`
	Data          interface{} `json:"data"`
	Links         []Link      `json:"links,omitempty"`
	MetaData      interface{} `json:"metadata,omitempty"`
}

Event encapsulates the data of an eventstore event.

EventStreamID is the id returned in the event atom response. EventNumber represents the stream version for this event. EventType describes the event type. EventID is the guid of the event. Data contains the data of the event. Links contains the urls of the event on the evenstore MetaData contains the metadata for the event.

func CreateTestEvent

func CreateTestEvent(stream, server, eventType string, eventNumber int, data *json.RawMessage, meta *json.RawMessage) *Event

CreateTestEvent will generate a test event.

The event data and meta will be a *json.RawMessage. The type of the event returned will be derived from the eventType argument. The event will have a single field named Foo which will contain random content which is simply a uuid string. The meta returned will contain a single field named Bar which will also contain a uuid string.

func CreateTestEventFromData

func CreateTestEventFromData(stream, server string, eventNumber int, data interface{}, meta interface{}) *Event

CreateTestEventFromData returns test events derived from the user specified data

Should be used where you require the simulator to return events of your own type with your own content.

func CreateTestEvents

func CreateTestEvents(numEvents int, stream string, server string, eventTypes ...string) []*Event

CreateTestEvents will return a slice of random test events.

The types of the events will be randomly selected from the event type names passed in to the variadic argument eventTypes

func (*Event) PrettyPrint

func (e *Event) PrettyPrint() string

PrettyPrint renders an indented json view of the Event object.

type EventAtomResponse

type EventAtomResponse struct {
	Title   string      `json:"title"`
	ID      string      `json:"id"`
	Updated TimeStr     `json:"updated"`
	Summary string      `json:"summary"`
	Content interface{} `json:"content"`
}

EventAtomResponse is used internally to unmarshall the raw response

func CreateTestEventAtomResponse

func CreateTestEventAtomResponse(e *Event, tm *TimeStr) (*EventAtomResponse, error)

CreateTestEventAtomResponse returns an *eventAtomResponse derived from the *Event argument e.

The updated time of the response will be set to the value of the *TimeStr argument tm. If tm is nil then the updated time will be set to now.

func (*EventAtomResponse) PrettyPrint

func (e *EventAtomResponse) PrettyPrint() string

PrettyPrint renders and indented json view of the eventAtomResponse

type EventResponse

type EventResponse struct {
	Title   string
	ID      string
	Updated TimeStr
	Summary string
	Event   *Event
}

EventResponse encapsulates the response for an event reflecting the atom response returned from the server which contains data in addition to the actual event when requested as content type application/vnd.eventstore.atom+json

For more information on the server response see: http://docs.geteventstore.com/http-api/3.7.0/reading-streams/

func CreateTestEventResponse

func CreateTestEventResponse(e *Event, tm *TimeStr) *EventResponse

CreateTestEventResponse will return an *EventResponse containing the event provided in the argument e.

The Updated field of the EventResponse will be set to the value of ht TimeString tm if it is provided otherwise it will be set to time.Now

func CreateTestEventResponses

func CreateTestEventResponses(events []*Event, tm *TimeStr) []*EventResponse

CreateTestEventResponses will return a slice of *EventResponse containing the events provided in the argument events.

The Updated field of the EventResponse will be set to the value of ht TimeString tm if it is provided otherwise it will be set to time.Now

func (*EventResponse) PrettyPrint

func (e *EventResponse) PrettyPrint() string

PrettyPrint renders an indented json view of the EventResponse.

type Link struct {
	URI      string `json:"uri"`
	Relation string `json:"relation"`
}

Link encapsulates url data for events.

type TimeStr

type TimeStr string

TimeStr is a type used to format feed dates.

func Time

func Time(t time.Time) TimeStr

Time returns a TimeStr version of the time.Time argument t.

Directories

Path Synopsis
internal
uuid
Package uuid provides implementation of Universally Unique Identifier (UUID).
Package uuid provides implementation of Universally Unique Identifier (UUID).

Jump to

Keyboard shortcuts

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