-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic parsing of JSON into schema
- Loading branch information
Showing
8 changed files
with
213 additions
and
31 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
[ | ||
{ | ||
"method": "GET", | ||
"path": "/", | ||
"status": 200, | ||
"response": { | ||
"success": true | ||
} | ||
}, | ||
{ | ||
"method": "GET", | ||
"path": "/users", | ||
"status": 200, | ||
"response": { | ||
"success": true, | ||
"ids": [1, 2, 3] | ||
} | ||
}, | ||
{ | ||
"method": "POST", | ||
"path": "/users", | ||
"status": 201, | ||
"response": { | ||
"success": true, | ||
"id": 4 | ||
} | ||
}, | ||
{ | ||
"method": "GET", | ||
"path": "/admin", | ||
"status": 401, | ||
"response": { | ||
"success": false, | ||
"message": "unauthorized" | ||
} | ||
} | ||
] |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package localroast | ||
|
||
type Schema struct { | ||
Method string | ||
Path string | ||
StatusCode int | ||
Method string | ||
Path string | ||
Status int | ||
} |
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,61 @@ | ||
package schema | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/caalberts/localroast" | ||
) | ||
|
||
func FromJSON(filepath string) ([]localroast.Schema, error) { | ||
f, err := ioutil.ReadFile(filepath) | ||
if err != nil { | ||
return []localroast.Schema{}, err | ||
} | ||
return BytesToSchema(f) | ||
} | ||
|
||
type stub struct { | ||
Method *string `json:"method"` | ||
Path *string `json:"path"` | ||
Status *int `json:"status"` | ||
Response json.RawMessage `json:"response"` | ||
} | ||
|
||
func BytesToSchema(bytes []byte) ([]localroast.Schema, error) { | ||
var stubs []stub | ||
err := json.Unmarshal(bytes, &stubs) | ||
if err != nil { | ||
return []localroast.Schema{}, err | ||
} | ||
|
||
schemas := make([]localroast.Schema, len(stubs)) | ||
for i, stub := range stubs { | ||
if f := missingFields(stub); len(f) > 0 { | ||
return []localroast.Schema{}, fmt.Errorf("Missing required fields: %s", strings.Join(f, ", ")) | ||
} | ||
schemas[i] = localroast.Schema{ | ||
Method: *stub.Method, | ||
Path: *stub.Path, | ||
Status: *stub.Status, | ||
} | ||
} | ||
|
||
return schemas, nil | ||
} | ||
|
||
func missingFields(s stub) []string { | ||
var missingFields []string | ||
if s.Method == nil { | ||
missingFields = append(missingFields, "method") | ||
} | ||
if s.Path == nil { | ||
missingFields = append(missingFields, "path") | ||
} | ||
if s.Status == nil { | ||
missingFields = append(missingFields, "status") | ||
} | ||
return missingFields | ||
} |
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,84 @@ | ||
package schema | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var validJSON, _ = ioutil.ReadFile("../examples/stubs.json") | ||
|
||
func TestBytesToSchema(t *testing.T) { | ||
schemas, err := BytesToSchema(validJSON) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 4, len(schemas)) | ||
|
||
assert.Equal(t, http.MethodGet, schemas[0].Method) | ||
assert.Equal(t, http.MethodGet, schemas[1].Method) | ||
assert.Equal(t, http.MethodPost, schemas[2].Method) | ||
assert.Equal(t, http.MethodGet, schemas[3].Method) | ||
|
||
assert.Equal(t, "/", schemas[0].Path) | ||
assert.Equal(t, "/users", schemas[1].Path) | ||
assert.Equal(t, "/users", schemas[2].Path) | ||
assert.Equal(t, "/admin", schemas[3].Path) | ||
|
||
assert.Equal(t, http.StatusOK, schemas[0].Status) | ||
assert.Equal(t, http.StatusOK, schemas[1].Status) | ||
assert.Equal(t, http.StatusCreated, schemas[2].Status) | ||
assert.Equal(t, http.StatusUnauthorized, schemas[3].Status) | ||
} | ||
|
||
var missingKeys = ` | ||
[ | ||
{ | ||
"response": { | ||
"success": true | ||
} | ||
} | ||
] | ||
` | ||
|
||
func TestJSONWithMissingKeys(t *testing.T) { | ||
_, err := BytesToSchema([]byte(missingKeys)) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "Missing required fields: method, path, status", err.Error()) | ||
} | ||
|
||
var invalidJSON = ` | ||
[ | ||
{ | ||
method: "GET", | ||
path: "/", | ||
status: 200, | ||
response: { | ||
"success": true | ||
} | ||
} | ||
] | ||
` | ||
|
||
func TestInvalidJSON(t *testing.T) { | ||
_, err := BytesToSchema([]byte(invalidJSON)) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
var jsonObject = ` | ||
{ | ||
"method": "POST", | ||
"path": "/users", | ||
"status": 201, | ||
"response": { | ||
"success": true, | ||
"id": 4 | ||
} | ||
} | ||
` | ||
|
||
func TestJSONObject(t *testing.T) { | ||
_, err := BytesToSchema([]byte(jsonObject)) | ||
assert.NotNil(t, err) | ||
} |
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