Browse Source

added behavior testing

Aaron Torres 2 years ago
parent
commit
63cb71fed8
3 changed files with 110 additions and 0 deletions
  1. 12 0
      chapter8/bdd/features/handler.feature
  2. 32 0
      chapter8/bdd/handler.go
  3. 66 0
      chapter8/bdd/handler_test.go

+ 12 - 0
chapter8/bdd/features/handler.feature

@@ -0,0 +1,12 @@
+Feature: Bad Method
+    Scenario: Good request
+        Given we create a HandlerRequest payload with:
+            | reader |
+            | coder  |
+            | other  |
+        And we POST the HandlerRequest to /hello
+        Then the response code should be 200
+        And the response body should be:
+            | BDD testing reader |
+            | BDD testing coder  |
+            | BDD testing other  |

+ 32 - 0
chapter8/bdd/handler.go

@@ -0,0 +1,32 @@
+package bdd
+
+import (
+	"encoding/json"
+	"fmt"
+	"net/http"
+)
+
+// HandlerRequest will be json decoded
+// into by Handler
+type HandlerRequest struct {
+	Name string `json:"name"`
+}
+
+// Handler takes a request and renders a response
+func Handler(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+	if r.Method != http.MethodPost {
+		w.WriteHeader(http.StatusMethodNotAllowed)
+		return
+	}
+
+	dec := json.NewDecoder(r.Body)
+	var req HandlerRequest
+	if err := dec.Decode(&req); err != nil {
+		w.WriteHeader(http.StatusBadRequest)
+		return
+	}
+
+	w.WriteHeader(http.StatusOK)
+	w.Write([]byte(fmt.Sprintf("BDD testing %s", req.Name)))
+}

+ 66 - 0
chapter8/bdd/handler_test.go

@@ -0,0 +1,66 @@
+package bdd
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"net/http/httptest"
+
+	"github.com/DATA-DOG/godog"
+	"github.com/DATA-DOG/godog/gherkin"
+)
+
+var payloads []HandlerRequest
+var resps []*httptest.ResponseRecorder
+
+func weCreateAHandlerRequestPayloadWith(arg1 *gherkin.DataTable) error {
+	for _, row := range arg1.Rows {
+		h := HandlerRequest{
+			Name: row.Cells[0].Value,
+		}
+		payloads = append(payloads, h)
+	}
+	return nil
+}
+
+func wePOSTTheHandlerRequestToHello() error {
+	for _, p := range payloads {
+		v, err := json.Marshal(p)
+		if err != nil {
+			return err
+		}
+		w := httptest.NewRecorder()
+		r := httptest.NewRequest("POST", "/hello", bytes.NewBuffer(v))
+
+		Handler(w, r)
+		resps = append(resps, w)
+	}
+	return nil
+}
+
+func theResponseCodeShouldBe(arg1 int) error {
+	for _, r := range resps {
+		if got, want := r.Code, arg1; got != want {
+			return fmt.Errorf("got: %d; want %d", got, want)
+		}
+	}
+	return nil
+}
+
+func theResponseBodyShouldBe(arg1 *gherkin.DataTable) error {
+	for c, row := range arg1.Rows {
+		b := bytes.Buffer{}
+		b.ReadFrom(resps[c].Body)
+		if got, want := b.String(), row.Cells[0].Value; got != want {
+			return fmt.Errorf("got: %s; want %s", got, want)
+		}
+	}
+	return nil
+}
+
+func FeatureContext(s *godog.Suite) {
+	s.Step(`^we create a HandlerRequest payload with:$`, weCreateAHandlerRequestPayloadWith)
+	s.Step(`^we POST the HandlerRequest to \/hello$`, wePOSTTheHandlerRequestToHello)
+	s.Step(`^the response code should be (\d+)$`, theResponseCodeShouldBe)
+	s.Step(`^the response body should be:$`, theResponseBodyShouldBe)
+}