Skip to content

Commit

Permalink
Calculate file paths(schema and body) based on imposter path
Browse files Browse the repository at this point in the history
  • Loading branch information
aperezg committed Apr 25, 2019
1 parent d90d093 commit bc924cd
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ imposters
schemas

!test/testdata/imposters/
!test/testdata/schemas/
!test/testdata/imposters/schemas/
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@

## v0.2.1 (2019/04/25)

* Allow imposter's matching by request schema
* Allow imposter's matching by request schema
* Dynamic responses based on regex endpoint or request schema
* Calculate files directory(body and schema) based on imposters path
* Update REAMDE.md with resolved features and new future features
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func writeBody(imposter Imposter, w http.ResponseWriter) {
wb := []byte(imposter.Response.Body)

if imposter.Response.BodyFile != nil {
wb = fetchBodyFromFile(*imposter.Response.BodyFile)
bodyFile := imposter.CalculateFilePath(*imposter.Response.BodyFile)
wb = fetchBodyFromFile(bodyFile)
}
w.Write(wb)
}
Expand Down
8 changes: 4 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ func TestImposterHandler(t *testing.T) {
var headers = make(http.Header)
headers.Add("Content-Type", "application/json")

schemaFile := "test/testdata/schemas/create_gopher_request.json"
bodyFile := "test/testdata/responses/create_gopher_response.json"
bodyFileFake := "test/testdata/responses/create_gopher_response_fail.json"
schemaFile := "test/testdata/imposters/schemas/create_gopher_request.json"
bodyFile := "test/testdata/imposters/responses/create_gopher_response.json"
bodyFileFake := "test/testdata/imposters/responses/create_gopher_response_fail.json"
body := `{"test":true}`

validRequest := Request{
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestInvalidHeaders(t *testing.T) {
}
}
}`)
schemaFile := "test/testdata/schemas/create_gopher_request.json"
schemaFile := "test/testdata/imposters/schemas/create_gopher_request.json"
var expectedHeaders = make(http.Header)
expectedHeaders.Add("Content-Type", "application/json")
expectedHeaders.Add("Authorization", "Bearer gopher")
Expand Down
19 changes: 14 additions & 5 deletions imposter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package killgrave

import "net/http"
import (
"net/http"
"path"
)

// Imposter define an imposter structure
type Imposter struct {
BasePath string
Request Request `json:"request"`
Response Response `json:"response"`
}

// CalculateFilePath calculate file path based on basePath of imposter directory
func (i *Imposter) CalculateFilePath(filePath string) string {
return path.Join(i.BasePath, filePath)
}

// Request represent the structure of real request
type Request struct {
Method string `json:"method"`
Expand All @@ -18,8 +27,8 @@ type Request struct {

// Response represent the structure of real response
type Response struct {
Status int `json:"status"`
Body string `json:"body"`
BodyFile *string `json:"bodyFile"`
Headers *http.Header `json:"headers"`
Status int `json:"status"`
Body string `json:"body"`
BodyFile *string `json:"bodyFile"`
Headers *http.Header `json:"headers"`
}
10 changes: 6 additions & 4 deletions route_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func MatcherBySchema(imposter Imposter) mux.MatcherFunc {
err := validateSchema(imposter, req)

// TODO: inject the logger
log.Println(err)

return err == nil
if err != nil {
log.Println(err)
return false
}
return true
}
}

Expand All @@ -35,7 +37,7 @@ func validateSchema(imposter Imposter, req *http.Request) error {
req.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()

schemaFile := *imposter.Request.SchemaFile
schemaFile := imposter.CalculateFilePath(*imposter.Request.SchemaFile)
if _, err := os.Stat(schemaFile); os.IsNotExist(err) {
return errors.Wrapf(err, "the schema file %s not found", schemaFile)
}
Expand Down
6 changes: 3 additions & 3 deletions route_matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestMatcherBySchema(t *testing.T) {
bodyA := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"gopher\"}")))
bodyB := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"cat\"}")))

schemaGopherFile := "test/testdata/schemas/type_gopher.json"
schemaCatFile := "test/testdata/schemas/type_cat.json"
schemeFailFile := "test/testdata/schemas/type_gopher_fail.json"
schemaGopherFile := "test/testdata/imposters/schemas/type_gopher.json"
schemaCatFile := "test/testdata/imposters/schemas/type_cat.json"
schemeFailFile := "test/testdata/imposters/schemas/type_gopher_fail.json"

requestWithoutSchema := Request{
Method: "POST",
Expand Down
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (s *Server) buildImposters() error {
files, _ := ioutil.ReadDir(s.impostersPath)

for _, f := range files {
if f.IsDir() {
continue
}

var imposter Imposter
if err := s.buildImposter(f.Name(), &imposter); err != nil {
return err
Expand All @@ -65,5 +69,7 @@ func (s *Server) buildImposter(imposterFileName string, imposter *Imposter) erro
if err := json.Unmarshal(bytes, imposter); err != nil {
return malformattedImposterError(fmt.Sprintf("error while unmarshall imposter file %s", f))
}
imposter.BasePath = s.impostersPath

return nil
}
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRunServer(t *testing.T) {
err error
}{
{"imposter directory not found", NewServer("failImposterPath", nil), invalidDirectoryError("error")},
{"malformatted json", NewServer("test/testdata/malformatted", nil), malformattedImposterError("error")},
{"malformatted json", NewServer("test/testdata/malformatted_imposters", nil), malformattedImposterError("error")},
{"valid imposter", NewServer("test/testdata/imposters", mux.NewRouter()), nil},
}

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit bc924cd

Please sign in to comment.