git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #76 from 18F/extract-api-package

Extract api package

commit41044ecac00f545c90e876b9e0883028d7f3b601 authorJehiah Czebotar <jehiah@gmail.com> dateMon 30 Mar 2015, 22:47 (12 years ago) parentfd74eae45286af
3 files changed+102−24
api/api.go+32 api/api_test.go+68 oauthproxy.go+2−24
api/api.gonew file+32
@@ -0,0 +1,32 @@
1+package api
2+
3+import (
4+ "errors"
5+ "io/ioutil"
6+ "log"
7+ "net/http"
8+
9+ "github.com/bitly/go-simplejson"
10+)
11+
12+func Request(req *http.Request) (*simplejson.Json, error) {
13+ httpclient := &http.Client{}
14+ resp, err := httpclient.Do(req)
15+ if err != nil {
16+ return nil, err
17+ }
18+ body, err := ioutil.ReadAll(resp.Body)
19+ resp.Body.Close()
20+ if err != nil {
21+ return nil, err
22+ }
23+ if resp.StatusCode != 200 {
24+ log.Printf("got response code %d - %s", resp.StatusCode, body)
25+ return nil, errors.New("api request returned non 200 status code")
26+ }
27+ data, err := simplejson.NewJson(body)
28+ if err != nil {
29+ return nil, err
30+ }
31+ return data, nil
32+}
api/api_test.gonew file+68
@@ -0,0 +1,68 @@
1+package api
2+
3+import (
4+ "github.com/bitly/go-simplejson"
5+ "github.com/bmizerany/assert"
6+ "net/http"
7+ "net/http/httptest"
8+ "strings"
9+ "testing"
10+)
11+
12+func testBackend(response_code int, payload string) *httptest.Server {
13+ return httptest.NewServer(http.HandlerFunc(
14+ func(w http.ResponseWriter, r *http.Request) {
15+ w.WriteHeader(response_code)
16+ w.Write([]byte(payload))
17+ }))
18+}
19+
20+func TestRequest(t *testing.T) {
21+ backend := testBackend(200, "{\"foo\": \"bar\"}")
22+ defer backend.Close()
23+
24+ req, _ := http.NewRequest("GET", backend.URL, nil)
25+ response, err := Request(req)
26+ assert.Equal(t, nil, err)
27+ result, err := response.Get("foo").String()
28+ assert.Equal(t, nil, err)
29+ assert.Equal(t, "bar", result)
30+}
31+
32+func TestRequestFailure(t *testing.T) {
33+ // Create a backend to generate a test URL, then close it to cause a
34+ // connection error.
35+ backend := testBackend(200, "{\"foo\": \"bar\"}")
36+ backend.Close()
37+
38+ req, err := http.NewRequest("GET", backend.URL, nil)
39+ assert.Equal(t, nil, err)
40+ resp, err := Request(req)
41+ assert.Equal(t, (*simplejson.Json)(nil), resp)
42+ assert.NotEqual(t, nil, err)
43+ if !strings.HasSuffix(err.Error(), "connection refused") {
44+ t.Error("expected error when a connection fails")
45+ }
46+}
47+
48+func TestHttpErrorCode(t *testing.T) {
49+ backend := testBackend(404, "{\"foo\": \"bar\"}")
50+ defer backend.Close()
51+
52+ req, err := http.NewRequest("GET", backend.URL, nil)
53+ assert.Equal(t, nil, err)
54+ resp, err := Request(req)
55+ assert.Equal(t, (*simplejson.Json)(nil), resp)
56+ assert.NotEqual(t, nil, err)
57+}
58+
59+func TestJsonParsingError(t *testing.T) {
60+ backend := testBackend(200, "not well-formed JSON")
61+ defer backend.Close()
62+
63+ req, err := http.NewRequest("GET", backend.URL, nil)
64+ assert.Equal(t, nil, err)
65+ resp, err := Request(req)
66+ assert.Equal(t, (*simplejson.Json)(nil), resp)
67+ assert.NotEqual(t, nil, err)
68+}
oauthproxy.go+2−24
@@ -6,7 +6,6 @@ import (
66 "errors"
77 "fmt"
88 "html/template"
9− "io/ioutil"
109 "log"
1110 "net"
1211 "net/http"
@@ -17,6 +16,7 @@ import (
1716 "time"
1817
1918 "github.com/bitly/go-simplejson"
19+ "github.com/bitly/google_auth_proxy/api"
2020)
2121
2222const pingPath = "/ping"
@@ -171,28 +171,6 @@ func (p *OauthProxy) GetLoginURL(host, redirect string) string {
171171 return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
172172}
173173
174−func apiRequest(req *http.Request) (*simplejson.Json, error) {
175− httpclient := &http.Client{}
176− resp, err := httpclient.Do(req)
177− if err != nil {
178− return nil, err
179− }
180− body, err := ioutil.ReadAll(resp.Body)
181− resp.Body.Close()
182− if err != nil {
183− return nil, err
184− }
185− if resp.StatusCode != 200 {
186− log.Printf("got response code %d - %s", resp.StatusCode, body)
187− return nil, errors.New("api request returned non 200 status code")
188− }
189− data, err := simplejson.NewJson(body)
190− if err != nil {
191− return nil, err
192− }
193− return data, nil
194−}
195−
196174func (p *OauthProxy) displayCustomLoginForm() bool {
197175 return p.HtpasswdFile != nil && p.DisplayHtpasswdForm
198176}
@@ -213,7 +191,7 @@ func (p *OauthProxy) redeemCode(host, code string) (string, string, error) {
213191 return "", "", err
214192 }
215193 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
216− json, err := apiRequest(req)
194+ json, err := api.Request(req)
217195 if err != nil {
218196 log.Printf("failed making request %s", err)
219197 return "", "", err