-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from friendsofgo/matching_by_request_schema
Matching by request schema and calculate files directories
- Loading branch information
Showing
16 changed files
with
204 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ imposters | |
schemas | ||
|
||
!test/testdata/imposters/ | ||
!test/testdata/schemas/ | ||
!test/testdata/imposters/schemas/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package killgrave | ||
|
||
import ( | ||
"bytes" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/xeipuuv/gojsonschema" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// MatcherBySchema check if the request matching with the schema file | ||
func MatcherBySchema(imposter Imposter) mux.MatcherFunc { | ||
return func(req *http.Request, rm *mux.RouteMatch) bool { | ||
err := validateSchema(imposter, req) | ||
|
||
// TODO: inject the logger | ||
if err != nil { | ||
log.Println(err) | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
|
||
func validateSchema(imposter Imposter, req *http.Request) error { | ||
if imposter.Request.SchemaFile == nil { | ||
return nil | ||
} | ||
|
||
var b []byte | ||
|
||
defer func() { | ||
req.Body.Close() | ||
req.Body = ioutil.NopCloser(bytes.NewBuffer(b)) | ||
}() | ||
|
||
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) | ||
} | ||
|
||
b, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
return errors.Wrapf(err, "impossible read the request body") | ||
} | ||
|
||
dir, _ := os.Getwd() | ||
schemaFilePath := "file://" + dir + "/" + schemaFile | ||
schema := gojsonschema.NewReferenceLoader(schemaFilePath) | ||
document := gojsonschema.NewStringLoader(string(b)) | ||
|
||
res, err := gojsonschema.Validate(schema, document) | ||
if err != nil { | ||
return errors.Wrap(err, "error validating the json schema") | ||
} | ||
|
||
if !res.Valid() { | ||
for _, desc := range res.Errors() { | ||
return errors.New(desc.String()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package killgrave | ||
|
||
import ( | ||
"bytes" | ||
"github.com/gorilla/mux" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestMatcherBySchema(t *testing.T) { | ||
bodyA := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"gopher\"}"))) | ||
bodyB := ioutil.NopCloser(bytes.NewReader([]byte("{\"type\": \"cat\"}"))) | ||
|
||
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", | ||
Endpoint: "/login", | ||
SchemaFile: nil, | ||
} | ||
|
||
requestWithSchema := Request{ | ||
Method: "POST", | ||
Endpoint: "/login", | ||
SchemaFile: &schemaGopherFile, | ||
} | ||
|
||
requestWithNonExistingSchema := Request{ | ||
Method: "POST", | ||
Endpoint: "/login", | ||
SchemaFile: &schemaCatFile, | ||
} | ||
|
||
requestWithWrongSchema := Request{ | ||
Method: "POST", | ||
Endpoint: "/login", | ||
SchemaFile: &schemeFailFile, | ||
} | ||
|
||
okResponse := Response{Status: http.StatusOK} | ||
|
||
var matcherData = []struct { | ||
name string | ||
fn mux.MatcherFunc | ||
req *http.Request | ||
res bool | ||
}{ | ||
{"imposter without request schema", MatcherBySchema(Imposter{Request: requestWithoutSchema, Response: okResponse}), &http.Request{Body: bodyA}, true}, | ||
{"correct request schema", MatcherBySchema(Imposter{Request: requestWithSchema, Response: okResponse}), &http.Request{Body: bodyA}, true}, | ||
{"incorrect request schema", MatcherBySchema(Imposter{Request: requestWithSchema, Response: okResponse}), &http.Request{Body: bodyB}, false}, | ||
{"non-existing schema file", MatcherBySchema(Imposter{Request: requestWithNonExistingSchema, Response: okResponse}), &http.Request{Body: bodyB}, false}, | ||
{"malformatted schema file", MatcherBySchema(Imposter{Request: requestWithWrongSchema, Response: okResponse}), &http.Request{Body: bodyB}, false}, | ||
} | ||
|
||
for _, tt := range matcherData { | ||
t.Run(tt.name, func(t *testing.T) { | ||
res := tt.fn(tt.req, nil) | ||
if res != tt.res { | ||
t.Fatalf("error while matching by request schema - expected: %t, given: %t", tt.res, res) | ||
} | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.