grim/resticide

Parents bc42441abfc1
Children 6cdca9324cf4
Allow downloading of file as well as checking their md5's and sha256's closes #9
--- a/assets/schema.json Wed Nov 11 23:09:49 2015 -0600
+++ b/assets/schema.json Thu Nov 12 22:52:08 2015 -0600
@@ -37,6 +37,20 @@
"type": "object",
"additionalProperties": true
},
+ "file": {
+ "type": "object",
+ "additionalProperties": false,
+ "properties": {
+ "filename": {"type": "string"},
+ "md5": {"type": "string"},
+ "sha256": {"type": "string"}
+ },
+ "anyOf": [{
+ "required": ["md5"]
+ }, {
+ "required": ["sha256"]
+ }]
+ },
"response": {
"type": "object",
"properties": {
@@ -55,6 +69,13 @@
}, {
"type": "null"
}]
+ },
+ "file": {
+ "oneOf": [{
+ "$ref": "#/definitions/file"
+ }, {
+ "type": "null"
+ }]
}
},
"anyOf": [{
--- a/test.go Wed Nov 11 23:09:49 2015 -0600
+++ b/test.go Thu Nov 12 22:52:08 2015 -0600
@@ -1,10 +1,11 @@
package main
import (
- _ "fmt"
-
"bytes"
+ "crypto/sha256"
+ "crypto/md5"
"encoding/json"
+ "fmt"
"io"
"io/ioutil"
"mime"
@@ -30,6 +31,7 @@
Headers map[string]string `json:"headers"`
Body string `json:"body"`
JSONSchema map[string]interface{} `json:"json-schema"`
+ File map[string]string `json:"file"`
}
type TestFormData struct {
@@ -171,24 +173,63 @@
}
}
+ // read the body into a byte array
body_byte, err := ioutil.ReadAll(hresp.Body)
if err != nil {
return
}
- body := string(body_byte[:])
+
+ // check if we have a file block and check it
+ if len(req.Response.File) > 0 {
+ passed := true
+
+ if sha256_digest, ok := req.Response.File["sha256"]; ok {
+ sum := sha256.Sum256(body_byte)
+
+ if fmt.Sprintf("%x", sum) != sha256_digest {
+ passed = false
+ }
+ }
+
+ if md5_digest, ok := req.Response.File["md5"]; ok {
+ sum := md5.Sum(body_byte)
- // check if we have a json schema to validate
- if len(req.Response.JSONSchema) != 0 {
- schema_loader := gojsonschema.NewGoLoader(req.Response.JSONSchema)
- body_loader := gojsonschema.NewStringLoader(body)
+ if fmt.Sprintf("%x", sum) != md5_digest {
+ passed = false
+ }
+ }
- schema_result, err := gojsonschema.Validate(schema_loader, body_loader)
- if err != nil {
+ if filename, ok := req.Response.File["filename"]; ok {
+ file, err := os.Create(filename)
+ if err != nil {
+ passed = false
+ }
+
+ _, err = file.Write(body_byte)
+ if err != nil {
+ passed = false
+ }
+ }
+
+ if passed == false {
return
}
+ } else {
+ body := string(body_byte[:])
- if schema_result.Valid() != true {
- return
+ // check if we have a json schema to validate
+ if len(req.Response.JSONSchema) != 0 {
+ schema_loader := gojsonschema.NewGoLoader(req.Response.JSONSchema)
+ body_loader := gojsonschema.NewStringLoader(body)
+
+ schema_result, err := gojsonschema.Validate(schema_loader, body_loader)
+ if err != nil {
+ return
+ }
+
+ if schema_result.Valid() != true {
+ return
+ }
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/httpbin-download-png.json Thu Nov 12 22:52:08 2015 -0600
@@ -0,0 +1,19 @@
+{
+ "name": "httpbin download png",
+ "request": {
+ "path": "/image/png",
+ "method": "GET",
+ "response": {
+ "statuscode": 200,
+ "headers": {
+ "Content-Type": "image/png",
+ "Content-Length": "8090"
+ },
+ "file": {
+ "md5": "5cca6069f68fbf739fce37e0963f21e7",
+ "sha256": "541a1ef5373be3dc49fc542fd9a65177b664aec01c8d8608f99e6ec95577d8c1",
+ "filename": "foo.png"
+ }
+ }
+ }
+}