git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #76 from 18F/extract-api-package
41044ecJehiah Czebotar
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 (
6 "errors"
6 "errors"
7 "fmt"
7 "fmt"
8 "html/template"
8 "html/template"
9− "io/ioutil"
10 "log"
9 "log"
11 "net"
10 "net"
12 "net/http"
11 "net/http"
@@ -17,6 +16,7 @@ import (
17 "time"
16 "time"
18
17
19 "github.com/bitly/go-simplejson"
18 "github.com/bitly/go-simplejson"
19+ "github.com/bitly/google_auth_proxy/api"
20)
20)
21
21
22const pingPath = "/ping"
22const pingPath = "/ping"
@@ -171,28 +171,6 @@ func (p *OauthProxy) GetLoginURL(host, redirect string) string {
171 return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
171 return fmt.Sprintf("%s?%s", p.oauthLoginUrl, params.Encode())
172}
172}
173
173
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−
196func (p *OauthProxy) displayCustomLoginForm() bool {
174func (p *OauthProxy) displayCustomLoginForm() bool {
197 return p.HtpasswdFile != nil && p.DisplayHtpasswdForm
175 return p.HtpasswdFile != nil && p.DisplayHtpasswdForm
198}
176}
@@ -213,7 +191,7 @@ func (p *OauthProxy) redeemCode(host, code string) (string, string, error) {
213 return "", "", err
191 return "", "", err
214 }
192 }
215 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
193 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
216− json, err := apiRequest(req)
194+ json, err := api.Request(req)
217 if err != nil {
195 if err != nil {
218 log.Printf("failed making request %s", err)
196 log.Printf("failed making request %s", err)
219 return "", "", err
197 return "", "", err