restify

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: MIT Imports: 8 Imported by: 0

README

go-restify

This package helps you automate API testing

Limitation

Only works for JSON API

Dependencies

"github.com/bastianrob/go-valuator"
"github.com/buger/jsonparser"

Example

Setting up test scenario
buffer := bytes.Buffer{}
scenario.New().Set().
    Name("Scenario One").
    Environment("Local").
    Description("").
    AddCase(restify.TestCase{
        Order:       1,
        Name:        "Setup Auth",
        Description: "Auth to firebase",
        Request: restify.Request{
            URL:    "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key={YOUR_FIREBASE_KEY}",
            Method: "POST",
            Payload: json.RawMessage(`{
                "email": "your.user@email.com",
                "password": "your.password",
                "returnSecureToken": true
            }`),
        },
        Expect: restify.Expect{
            StatusCode: 200,
            Evaluate: []restify.Expression{{
                Prop:        "idToken",
                Operator:    "!=",
                Value:       "",
                Description: "ID Token must be returned from firebase",
            }},
        },
        Pipeline: restify.Pipeline{
            Cache:     true,
            CacheAs:   "auth",
            OnFailure: onfailure.Exit,
        },
    }).
    AddCase(restify.TestCase{
        Order:       2,
        Name:        "Get List",
        Description: "Get a list of resource that returns {data: [{resource1, resource2}]}",
        Request: restify.Request{
            URL:    "http://localhost:3000/resources",
            Method: "GET",
            Headers: map[string]string{
                "Authorization": "Bearer {auth.idToken}",
            },
            Payload: nil,
        },
        Expect: restify.Expect{
            StatusCode:       200,
            Evaluate: []restify.Expression{{
                Object:      "data.[0]",
                Prop:        "id",
                Operator:    "!=",
                Value:       "",
                Description: "Returned data is not empty",
            }},
        },
        Pipeline: restify.Pipeline{
            Cache:     true,
            CacheAs:   "R1",
            OnFailure: onfailure.Exit,
        },
    }).
    AddCase(restify.TestCase{
        Order:       3,
        Name:        "Get One",
        Description: "Get one resource from previous case",
        Request: restify.Request{
            URL:    "http://localhost:3000/resources/{R1.data.[0].id}",
            Method: "GET",
            Headers: map[string]string{
                "Authorization": "Bearer {auth.idToken}",
            },
            Payload: nil,
        },
        Expect: restify.Expect{
            StatusCode:       200,
            Evaluate: []restify.Expression{{
                Object:      "data",
                Prop:        "id",
                Operator:    "!=",
                Value:       "",
                Description: "Returned data is not empty",
            }},
        },
        Pipeline: restify.Pipeline{
            Cache:     true,
            CacheAs:   "R2",
            OnFailure: onfailure.Exit,
        },
    }).End().
    Run(&buffer)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Expect

type Expect struct {
	StatusCode int               `json:"status_code" bson:"status_code"`
	Headers    map[string]string `json:"headers" bson:"headers"`
	Evaluate   []Expression      `json:"evaluate" bson:"evaluate"`
}

Expect response expectation

func (*Expect) Parse

func (e *Expect) Parse(cache map[string]json.RawMessage)

Parse cache into evaluation value This will replace {....} with existing value in cache

type Expression

type Expression struct {
	Object      string `json:"object" bson:"object"`
	Prop        string `json:"prop" bson:"prop"`
	Operator    string `json:"operator" bson:"operator"`
	Value       string `json:"value" bson:"value"`
	Description string `json:"description" bson:"description"`
}

Expression rule of expected response

type Pipeline

type Pipeline struct {
	Cache     bool           `json:"cache" bson:"cache"`
	CacheAs   string         `json:"cache_as" bson:"cache_as"`
	OnFailure enum.OnFailure `json:"on_failure" bson:"on_failure"`
}

Pipeline test pipeline as what to do with the response object

type Request

type Request struct {
	URL     string                 `json:"url" bson:"url"`
	Method  string                 `json:"method" bson:"method"`
	Headers map[string]string      `json:"headers" bson:"headers"`
	Payload map[string]interface{} `json:"payload" bson:"payload"`
}

Request test object

func (*Request) Parse

func (r *Request) Parse(cache map[string]json.RawMessage)

Parse cache into request parameter This will replace {....} with existing value in cache

type Scenario

type Scenario interface {
	Get() ScenarioGetter
	Set() ScenarioSetter
	Run(w io.Writer) []TestResult
}

Scenario is the biggest scope of a test Can have multiple test cases

type ScenarioGetter

type ScenarioGetter interface {
	ID() string
	Name() string
	Description() string
	Environment() string
	Cases() []TestCase
}

ScenarioGetter access to get scenario properties

type ScenarioSetter

type ScenarioSetter interface {
	ID(id string) ScenarioSetter
	Name(name string) ScenarioSetter
	Description(desc string) ScenarioSetter
	Environment(env string) ScenarioSetter
	AddCase(tcase TestCase) ScenarioSetter
	End() Scenario
}

ScenarioSetter access to set scenario properties

type TestCase

type TestCase struct {
	Order       uint     `json:"order" bson:"order"`
	Name        string   `json:"name" bson:"name"`
	Description string   `json:"description" bson:"description"`
	Request     Request  `json:"request" bson:"request"`
	Expect      Expect   `json:"expect" bson:"expect"`
	Pipeline    Pipeline `json:"pipeline" bson:"pipeline"`
}

TestCase struct

type TestResult

type TestResult struct {
	Timestamp     int64  `json:"timestamp" bson:"timestamp"`
	ScenarioName  string `json:"scenario_name" bson:"scenario_name"`
	TestCaseOrder int    `json:"test_case_order" bson:"test_case_order"`
	TestCaseName  string `json:"test_case_name" bson:"test_case_name"`
	RequestMethod string `json:"request_method" bson:"request_method"`
	RequestURL    string `json:"request_url" bson:"request_url"`
	//RequestPayload?
	ResponseCode int   `json:"response_code" bson:"response_code"`
	ResponseSize int64 `json:"response_size" bson:"response_size"`
	//Response timing
	TimingDNS       time.Duration `json:"timing_dns" bson:"timing_dns"`
	TimingHandshake time.Duration `json:"timing_handshake" bson:"timing_handshake"`
	TimingConnected time.Duration `json:"timing_connected" bson:"timing_connected"`
	TimingFirstByte time.Duration `json:"timing_first_byte" bson:"timing_first_byte"`
	TimingTotal     time.Duration `json:"timing_total" bson:"timing_total"`
	//ResponsePayload?
	ExpectedCode int    `json:"expected_code" bson:"expected_code"`
	Message      string `json:"message" bson:"message"`
}

TestResult object, all properties must be flat (not nested / denormalized) Hopefully de-normalizing the structure can make analytic easier Generally immutable, so we don't need ID and just let DB auto gen for us

func NewTestResult

func NewTestResult(scn Scenario, tcn int) TestResult

NewTestResult from scenario

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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