Skip to content

Instantly share code, notes, and snippets.

@kpurdon
Last active June 13, 2016 02:03
Show Gist options
  • Select an option

  • Save kpurdon/403eb0483ba8e41fbbf6cca6a45f3ec0 to your computer and use it in GitHub Desktop.

Select an option

Save kpurdon/403eb0483ba8e41fbbf6cca6a45f3ec0 to your computer and use it in GitHub Desktop.

Revisions

  1. kpurdon revised this gist Jun 13, 2016. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -27,12 +27,7 @@ func TestAuth(t *testing.T) {
    url: "/ping?user=test&password=badpassword",
    expectedBody: "NOT_AUTHORIZED\n",
    expectedCode: 403,
    }, // {
    // description: "success",
    // url: "/ping?user=test&password=abc123",
    // expectedBody: "OK\n",
    // expectedCode: 200,
    // },
    },
    }

    ts := httptest.NewServer(Auth(GetTestHandler()))
  2. kpurdon created this gist May 15, 2016.
    58 changes: 58 additions & 0 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    func TestAuth(t *testing.T) {
    assert := assert.New(t)

    tests := []struct {
    description string
    url string
    expectedBody string
    expectedCode int
    }{
    {
    description: "missing arg user",
    url: "/ping",
    expectedBody: "MISSING_ARG_USER\n",
    expectedCode: 400,
    }, {
    description: "bad user",
    url: "/ping?user=baduser",
    expectedBody: "NOT_AUTHORIZED\n",
    expectedCode: 403,
    }, {
    description: "missing arg password",
    url: "/ping?user=test",
    expectedBody: "MISSING_ARG_PASSWORD\n",
    expectedCode: 400,
    }, {
    description: "bad password",
    url: "/ping?user=test&password=badpassword",
    expectedBody: "NOT_AUTHORIZED\n",
    expectedCode: 403,
    }, // {
    // description: "success",
    // url: "/ping?user=test&password=abc123",
    // expectedBody: "OK\n",
    // expectedCode: 200,
    // },
    }

    ts := httptest.NewServer(Auth(GetTestHandler()))
    defer ts.Close()

    for _, tc := range tests {
    var u bytes.Buffer
    u.WriteString(string(ts.URL))
    u.WriteString(tc.url)

    res, err := http.Get(u.String())
    assert.NoError(err)
    if res != nil {
    defer res.Body.Close()
    }

    b, err := ioutil.ReadAll(res.Body)
    assert.NoError(err)

    assert.Equal(tc.expectedCode, res.StatusCode, tc.description)
    assert.Equal(tc.expectedBody, string(b), tc.description)
    }
    }