Skip to content

Commit

Permalink
Merge pull request #14 from twharmon/tests
Browse files Browse the repository at this point in the history
Add some test cases
  • Loading branch information
twharmon authored Apr 11, 2022
2 parents db48a7c + 22131eb commit 53a9e01
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 0 deletions.
189 changes: 189 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,192 @@ func TestContextLogSilent(t *testing.T) {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogCriticalNone(t *testing.T) {
fname := "TestContextLogCriticalNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogCritical("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogAlertNone(t *testing.T) {
fname := "TestContextLogAlertNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogAlert("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogErrorNone(t *testing.T) {
fname := "TestContextLogErrorNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogError("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogNoticeNone(t *testing.T) {
fname := "TestContextLogNoticeNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogNotice("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogInfoNone(t *testing.T) {
fname := "TestContextLogInfoNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogInfo("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogDebugNone(t *testing.T) {
fname := "TestContextLogDebugNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogDebug("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestContextLogWarningNone(t *testing.T) {
fname := "TestContextLogWarningNone"
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer func() {
f.Close()
os.Remove(fname)
}()
want := ""
os.Stdout = f
ctx := &handlerContext{
logger: NewDefaultLogger(),
logLevel: LogLevelSilent,
}
ctx.LogWarning("foo %s", "bar")
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("unexpected err: %s", err)
}
got := string(b)
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}
25 changes: 25 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golamb

import (
"reflect"
"testing"

"github.com/aws/aws-lambda-go/events"
Expand Down Expand Up @@ -84,3 +85,27 @@ func TestRequestHeader(t *testing.T) {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestRequestRawPath(t *testing.T) {
want := "/foo"
ctx := &handlerContext{
req: &request{request: &events.APIGatewayV2HTTPRequest{RawPath: want}},
sp: &awsServiceProvider{config: &AWSServiceProviderConfig{}},
}
got := ctx.Request().RawPath()
if want != got {
t.Fatalf("want %v; got %v", want, got)
}
}

func TestRequestHeaders(t *testing.T) {
want := map[string]string{"foo": "bar"}
ctx := &handlerContext{
req: &request{request: &events.APIGatewayV2HTTPRequest{Headers: want}},
sp: &awsServiceProvider{config: &AWSServiceProviderConfig{}},
}
got := ctx.Request().Headers()
if !reflect.DeepEqual(want, got) {
t.Fatalf("want %v; got %v", want, got)
}
}

0 comments on commit 53a9e01

Please sign in to comment.