Skip to content

Commit

Permalink
Revert "Develop (#3)"
Browse files Browse the repository at this point in the history
This reverts commit f36d73d.
  • Loading branch information
ViaoV authored Aug 13, 2017
1 parent f36d73d commit 8006048
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 553 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
vendor/
dist/
testdata/
44 changes: 4 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
[![Build
Status](https://travis-ci.org/5Sigma/spyder.svg?branch=master)](https://travis-ci.org/5Sigma/spyder)

# Spyder
# spyder
API Testing and Request Framework

## Installation

### OSX

On OSX, spyder can be install with brew:

```
brew install 5sigma/tap/spyder
```

### Linux

Download the linux package from for the latest release:

https://github.com/5Sigma/spyder/releases/latest

### Windows

Windows binaries can be found in the release:

https://github.com/5Sigma/spyder/releases/latest

## API Testing and Requests

Expand Down Expand Up @@ -96,7 +75,7 @@ For POST requests the node is submitted as stringified JSON in the post body.
}
```

For more information about endpoints check out the [Endpoint Configuration Reference](https://github.com/5Sigma/spyder/wiki/Endpoint-Configuration-Reference)
## Handling dynamic data

The easiest way of handling dynamic data is by using variables directly inside
the configuration. There are two configuration files:
Expand Down Expand Up @@ -196,21 +175,6 @@ This request uses a transform script located at `scripts/signRequest.js`. That
could look like:

```js
signature = $hmac($variables.get('session_token_secret'), $request.body);
$request.headers.set('Authorization', $variables.get('session_token_id') + ':' + signature)
signature = $hmac($variables.get('session_token_secret'), $payload.get());
$headers.set('Authorization', $variables.get('session_token_id') + ':' + signature)
```

For more information on scripting see the [Scripting Reference](https://github.com/5Sigma/spyder/wiki/Script-Reference)


# Stress testing

Endpoints can be rapidly requested for stress testing using the `hammer`
command. The request will be made a number of times specified by the count
flag, or 100 times by default.

```
spyder hammer --count 1000 myEndpoint
```

For more information on scripting see the [Scripting Reference](https://github.com/5Sigma/spyder/wiki/Script-Reference)
77 changes: 0 additions & 77 deletions cmd/hammer.go

This file was deleted.

4 changes: 2 additions & 2 deletions cmd/request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"github.com/5sigma/spyder/config"
"github.com/5sigma/spyder/endpoint"
"github.com/5sigma/spyder/explorer"
"github.com/5sigma/spyder/output"
Expand All @@ -25,7 +24,7 @@ requested using:
$ spyder request sessions/auth
`,
Run: func(cmd *cobra.Command, args []string) {
config, err := endpoint.Load(path.Join(config.ProjectPath, "endpoints", args[0]+".json"))
config, err := endpoint.Load(path.Join("endpoints", args[0]+".json"))
if err != nil {
output.PrintFatal(err)
}
Expand All @@ -34,6 +33,7 @@ $ spyder request sessions/auth
if err != nil {
output.PrintFatal(err)
}

explorer.Start(args[0], config, res)

},
Expand Down
19 changes: 2 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ var LocalConfig = loadConfigFile("config.local.json")
// GlobalConfig - The global configuration read from config.json
var GlobalConfig = loadConfigFile("config.json")

// The path to the project root
var ProjectPath = "."

// InMemory - When true the config will not write to the disk. This is used for
// testing.
var InMemory = false
Expand Down Expand Up @@ -64,27 +61,15 @@ func ExpandString(str string) string {

// LoadConfigFile - Loads a config from a file on the disk.
func loadConfigFile(filename string) *Config {
var (
c *Config
)
if InMemory {
return loadDefaultConfig()
}

if _, err := os.Stat(filename); !os.IsNotExist(err) {
bytes, _ := ioutil.ReadFile(filename)
if strings.TrimSpace(string(bytes)) == "" {
c = loadDefaultConfig()
c.Filename = filename
return c
}
c = LoadConfig(bytes)
c.Filename = filename
return c
return LoadConfig(bytes)
}
c = loadDefaultConfig()
c.Filename = filename
return c
return loadDefaultConfig()
}

// Loads a config from a byte array.
Expand Down
7 changes: 0 additions & 7 deletions endpoint/config.local.json

This file was deleted.

41 changes: 15 additions & 26 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,9 @@ type (
Url string
OnComplete []string
Transform []string
Headers map[string][]string
}
)

func New() *EndpointConfig {
return &EndpointConfig{
json: &gabs.Container{},
OnComplete: []string{},
Transform: []string{},
}
}

// Load - Loads a confugruation from a file on the disk.
func Load(filename string) (*EndpointConfig, error) {
var (
Expand Down Expand Up @@ -60,19 +51,12 @@ func LoadBytes(fileBytes []byte) (*EndpointConfig, error) {
method, _ := jsonObject.Path("method").Data().(string)
url, _ := jsonObject.Path("url").Data().(string)

headerMap := make(map[string][]string)
children, _ := jsonObject.S("headers").ChildrenMap()
for key, child := range children {
headerMap[key] = []string{config.ExpandString(child.Data().(string))}
}

epConfig = &EndpointConfig{
json: jsonObject,
Method: method,
Url: url,
OnComplete: []string{},
Transform: []string{},
Headers: headerMap,
}

transformNodes, _ := jsonObject.S("transform").Children()
Expand All @@ -96,17 +80,24 @@ func (ep *EndpointConfig) GetString(path string) string {

// GetJSONString - returns the inner JSON at the path as a string.
func (ep *EndpointConfig) GetJSONString(path string) string {
if ep.json.Exists("data") {
return ep.json.Path("data").String()
}
return ""
return ep.json.Path("data").String()
}

// GetJSONBytes - returns the inner JSON at the path as a byte array.
func (ep *EndpointConfig) GetJSONBytes(path string) []byte {
return ep.json.Path("data").Bytes()
}

// Headers - returns the configured request headers as a string map
func (ep *EndpointConfig) Headers() map[string][]string {
headerMap := make(map[string][]string)
children, _ := ep.json.S("headers").ChildrenMap()
for key, child := range children {
headerMap[key] = []string{config.ExpandString(child.Data().(string))}
}
return headerMap
}

// RequestMethod - returns the request method.
func (ep *EndpointConfig) RequestMethod() string {
method := strings.ToUpper(ep.GetString("method"))
Expand All @@ -116,8 +107,8 @@ func (ep *EndpointConfig) RequestMethod() string {
// RequestURL - returns the full url for the request. If this is a GET request
// and has request parameters they are included in the URL.
func (ep *EndpointConfig) RequestURL() string {
if ep.RequestMethod() == "GET" {
baseURL, _ := url.Parse(expandFakes(config.ExpandString(ep.Url)))
if ep.Method == "GET" {
baseURL, _ := url.Parse(config.ExpandString(ep.Url))
params := url.Values{}
for k, v := range ep.GetRequestParams() {
params.Add(k, v)
Expand All @@ -138,17 +129,15 @@ func (ep *EndpointConfig) GetRequestParams() map[string]string {
paramsMap := make(map[string]string)
children, _ := ep.json.S("data").ChildrenMap()
for key, child := range children {
paramsMap[key] = expandFakes(config.ExpandString(child.Data().(string)))
paramsMap[key] = config.ExpandString(child.Data().(string))
}
return paramsMap
}

// RequestData - returns the data attribute from the config. This contains the
// payload, for a POST request, that will be sent to the server.
func (ep *EndpointConfig) RequestData() []byte {
dataJSON := ep.GetJSONString("data")
dataJSON = config.ExpandString(dataJSON)
dataJSON = expandFakes(dataJSON)
dataJSON := config.ExpandString(ep.GetJSONString("data"))
return []byte(dataJSON)
}

Expand Down
10 changes: 3 additions & 7 deletions endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestRequestUrl(t *testing.T) {
}

// POST request
json.Set("post", "method")
ep, _ = LoadBytes(json.Bytes())
ep.Method = "POST"
if ep.RequestURL() != ep.Url {
t.Errorf("Request URL missmatch:\nExpecting: %s\nReceived: %s", ep.Url,
ep.RequestURL())
Expand All @@ -51,11 +50,8 @@ func TestRequestUrl(t *testing.T) {
// GET request with variable expansion
config.LocalConfig.SetVariable("var", "value1")
config.LocalConfig.SetVariable("host", "127.0.0.1")
json = buildConfig()
params, _ = json.Object("data")
params.Set("$var", "option2")
params.Set("3", "option1")
json.Set("http://$host/api/endpoint", "url")
params.Set("$var", "option2")
ep, _ = LoadBytes(json.Bytes())
expectedUrl = "http://127.0.0.1/api/endpoint?option1=3&option2=value1"
if ep.RequestURL() != expectedUrl {
Expand All @@ -76,7 +72,7 @@ func TestHeaders(t *testing.T) {
t.Fatalf("Error reading config: %s", err.Error())
}

headerMap := ep.Headers
headerMap := ep.Headers()
contentTypeValues := headerMap["Content-Type"]
if contentTypeValues[0] != "application/json" {
t.Errorf("Header not stored or retrieved correctly")
Expand Down
Loading

0 comments on commit 8006048

Please sign in to comment.