Skip to content

Commit

Permalink
Merge branch 'release/0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rumyantseva committed May 22, 2017
2 parents d76091f + 0307fbd commit aa841af
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ The simplest way to make a web-server: register a handler function and listen on

http.HandleFunc("/", home)
http.ListenAndServe(":8000", nil)

## Step 03. Add tests

`TestHandler` function (see `handlers_test.go`) is the simplest way to test our web-server: check if the handler
returns expected result. Use `go test -v` command to run test.
36 changes: 36 additions & 0 deletions handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

// TestHandler is the simplest test: check base (/) URL
func TestHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(home))
defer ts.Close()

res, err := http.Get(ts.URL + "/")
if err != nil {
t.Fatal(err)
}

greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()

if err != nil {
t.Fatal(err)
}

expectedGreeting := "Processing URL /..."
testingGreeting := strings.Trim(string(greeting), " \n")
if testingGreeting != expectedGreeting {
t.Fatalf(
"Wrong greeting '%s', expected '%s'",
testingGreeting, expectedGreeting,
)
}
}

0 comments on commit aa841af

Please sign in to comment.