Skip to content

Commit

Permalink
example/php: Improve tests
Browse files Browse the repository at this point in the history
* Merge variable declaration with definiton for better style.
* properely handle end process error in test, if any
  • Loading branch information
yookoala committed Oct 13, 2020
1 parent 7c83675 commit 2b7bad8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions example/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func init() {
username = os.Getenv("USER")
}

func checkError(t *testing.T, cb func() error) {
if err := cb(); err != nil {
t.Errorf("Unexpected error: %s", err)
}
}

func isExamplePath(testPath string) bool {
if _, err := os.Stat(testPath); os.IsNotExist(err) {
return false
Expand Down Expand Up @@ -81,8 +87,7 @@ func get(h http.Handler, path string) (w *httptest.ResponseRecorder, err error)
}

func post(h http.Handler, path string, payload string) (w *httptest.ResponseRecorder, err error) {
var reader io.Reader
reader = strings.NewReader(payload)
var reader io.Reader = strings.NewReader(payload)
r, err := http.NewRequest("POST", path, reader)
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
r.Header.Add("Content-Length", fmt.Sprintf("%d", len(payload)))
Expand Down Expand Up @@ -123,7 +128,7 @@ func initEnv(t *testing.T, name string, worker int) (exmpPath string, process *p
func TestNewSimpleHandler(t *testing.T) {

exmpPath, process := initEnv(t, "phpfpm1", 10)
defer process.Stop()
defer checkError(t, process.Stop)

// start the proxy handler
network, address := process.Address()
Expand Down Expand Up @@ -190,7 +195,7 @@ func TestNewSimpleHandler(t *testing.T) {
func TestNewSimpleHandler__ErrorStream(t *testing.T) {

exmpPath, process := initEnv(t, "phpfpm2", 1) // 1 worker to test regression
defer process.Stop()
defer checkError(t, process.Stop)

// start the proxy handler
network, address := process.Address()
Expand Down Expand Up @@ -222,7 +227,7 @@ func TestNewSimpleHandler__ErrorStream(t *testing.T) {
func TestNewFileEndpointHandler(t *testing.T) {

exmpPath, process := initEnv(t, "phpfpm3", 10)
defer process.Stop()
defer checkError(t, process.Stop)

// start the proxy handler
var w *httptest.ResponseRecorder
Expand Down

0 comments on commit 2b7bad8

Please sign in to comment.