Last active
April 19, 2019 09:35
-
-
Save mblarsen/ff43452cdfab700e64b4318278316fe0 to your computer and use it in GitHub Desktop.
A small CLI wrapper + Go test wrapper for @serverless/event-mocks
This file contains hidden or 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
func TestHandler(t *testing.T) { | |
req := api.Req{} | |
reqBody, _ := json.Marshal(map[string]interface{}{ | |
"requestContext": map[string]interface{}{ | |
"Authorizer": map[string]interface{}{ | |
"claims": map[string]interface{}{ | |
"cognito:username": "my user", | |
}, | |
}, | |
}, | |
}) | |
assert.MockEvent(t, "apiGateway", string(reqBody), &req) | |
claims := req.RequestContext.Authorizer["claims"].(map[string]interface{}) | |
assert.Equals(t, claims["cognito:username"], "my user") | |
res, err := Handler(request) | |
// assert the response from your lambda | |
} |
This file contains hidden or 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
#!/usr/bin/env node | |
const createEvent = require("@serverless/event-mocks").default; | |
const type = process.argv[2]; | |
process.stdin.resume(); | |
process.stdin.setEncoding("utf8"); | |
process.stdin.on("data", function(data) { | |
try { | |
data = JSON.parse(data); | |
process.stdout.write(JSON.stringify(createEvent(`aws:${type}`, data))); | |
process.exit(0); | |
} catch (e) { | |
console.log("Invalid input: " + e.message); | |
process.exit(1); | |
} | |
}); | |
setTimeout(() => { | |
process.stdin.emit("data", "{}"); | |
}, 50); | |
/* vim :set ft=js */ |
This file contains hidden or 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
package assert | |
import ( | |
"bytes" | |
"encoding/json" | |
"os/exec" | |
"path/filepath" | |
"runtime" | |
"testing" | |
) | |
// MockEvent creates a mock event of one of the following types: | |
// alexaSkill, alexaSmartphone, apiGateway, cloudWatch, cloudWatchLog, | |
// cognitoUserPool, dynamo, iot, kinesis, s3, scheduled, ses, sns, websocket | |
func MockEvent(t *testing.T, eventType string, body string, event interface{}) error { | |
t.Helper() | |
echo := exec.Command("echo", body) | |
gen := exec.Command(getGenPath(), eventType) | |
gen.Stdin, _ = echo.StdoutPipe() | |
var outBuffer bytes.Buffer | |
gen.Stdout = &outBuffer | |
_ = gen.Start() | |
_ = echo.Run() | |
_ = gen.Wait() | |
return json.Unmarshal(outBuffer.Bytes(), &event) | |
} | |
func getGenPath() string { | |
_, filename, _, _ := runtime.Caller(1) | |
genPath, _ := filepath.Abs(filepath.Join(filepath.Dir(filename), "../../genevent")) | |
return genPath | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example command-line usage:
output: