Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
feat: asserting proper exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
jones2026 committed Aug 31, 2019
1 parent 1514211 commit 1d5910e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ steps:
- name: test
image: golang:1.12
commands:
- go test -v -cover
- "CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags \"-static\"' ."

- name: publish
Expand Down
38 changes: 38 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

func TestExitWithBadResponseStatus(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()

var raw []byte

origLogFatalf := logFatalf

// After this test, replace the original fatal function
defer func() { logFatalf = origLogFatalf }()

var actualError string
logFatalf = func(format string, args ...interface{}) {
if len(args) > 0 {
actualError = fmt.Sprintf(format, args)
} else {
actualError = format
}
}

postMessage(raw, nil, ts.URL)
expectedError := "Failed to post message, flowdock api returned: [503 Service Unavailable]"
if actualError == expectedError {
return
}
t.Fatalf("Expected error:\n%s\ngot:\n%s", expectedError, actualError)
}

0 comments on commit 1d5910e

Please sign in to comment.