OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package middleware |
| 6 |
| 7 import ( |
| 8 "net/http" |
| 9 "net/http/httptest" |
| 10 "testing" |
| 11 |
| 12 "github.com/julienschmidt/httprouter" |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 "golang.org/x/net/context" |
| 15 ) |
| 16 |
| 17 func init() { |
| 18 // disable this so that we can actually check the logic in these middlew
ares |
| 19 devAppserverBypassFn = func(context.Context) bool { return false } |
| 20 } |
| 21 |
| 22 func TestRequireCron(t *testing.T) { |
| 23 t.Parallel() |
| 24 |
| 25 Convey("Test RequireCron", t, func() { |
| 26 hit := false |
| 27 f := func(c context.Context, rw http.ResponseWriter, r *http.Req
uest, p httprouter.Params) { |
| 28 hit = true |
| 29 rw.Write([]byte("ok")) |
| 30 } |
| 31 |
| 32 Convey("from non-cron fails", func() { |
| 33 rec := httptest.NewRecorder() |
| 34 BaseTest(RequireCron(f))(rec, &http.Request{}, nil) |
| 35 So(hit, ShouldBeFalse) |
| 36 So(rec.Body.String(), ShouldEqual, "error: must be run f
rom cron") |
| 37 So(rec.Code, ShouldEqual, http.StatusForbidden) |
| 38 }) |
| 39 |
| 40 Convey("from cron succeeds", func() { |
| 41 rec := httptest.NewRecorder() |
| 42 BaseTest(RequireCron(f))(rec, &http.Request{ |
| 43 Header: http.Header{ |
| 44 http.CanonicalHeaderKey("x-appengine-cro
n"): []string{"true"}, |
| 45 }, |
| 46 }, nil) |
| 47 So(hit, ShouldBeTrue) |
| 48 So(rec.Body.String(), ShouldEqual, "ok") |
| 49 So(rec.Code, ShouldEqual, http.StatusOK) |
| 50 }) |
| 51 }) |
| 52 } |
| 53 |
| 54 func TestRequireTQ(t *testing.T) { |
| 55 t.Parallel() |
| 56 |
| 57 Convey("Test RequireTQ", t, func() { |
| 58 hit := false |
| 59 f := func(c context.Context, rw http.ResponseWriter, r *http.Req
uest, p httprouter.Params) { |
| 60 hit = true |
| 61 rw.Write([]byte("ok")) |
| 62 } |
| 63 |
| 64 Convey("from non-tq fails (wat)", func() { |
| 65 rec := httptest.NewRecorder() |
| 66 BaseTest(RequireTaskQueue("wat", f))(rec, &http.Request{
}, nil) |
| 67 So(hit, ShouldBeFalse) |
| 68 So(rec.Body.String(), ShouldEqual, "error: must be run f
rom the correct taskqueue") |
| 69 So(rec.Code, ShouldEqual, http.StatusForbidden) |
| 70 }) |
| 71 |
| 72 Convey("from non-tq fails", func() { |
| 73 rec := httptest.NewRecorder() |
| 74 BaseTest(RequireTaskQueue("", f))(rec, &http.Request{},
nil) |
| 75 So(hit, ShouldBeFalse) |
| 76 So(rec.Body.String(), ShouldEqual, "error: must be run f
rom the correct taskqueue") |
| 77 So(rec.Code, ShouldEqual, http.StatusForbidden) |
| 78 }) |
| 79 |
| 80 Convey("from wrong tq fails (wat)", func() { |
| 81 rec := httptest.NewRecorder() |
| 82 BaseTest(RequireTaskQueue("wat", f))(rec, &http.Request{ |
| 83 Header: http.Header{ |
| 84 http.CanonicalHeaderKey("x-appengine-que
uename"): []string{"else"}, |
| 85 }, |
| 86 }, nil) |
| 87 So(hit, ShouldBeFalse) |
| 88 So(rec.Body.String(), ShouldEqual, "error: must be run f
rom the correct taskqueue") |
| 89 So(rec.Code, ShouldEqual, http.StatusForbidden) |
| 90 }) |
| 91 |
| 92 Convey("from right tq succeeds (wat)", func() { |
| 93 rec := httptest.NewRecorder() |
| 94 BaseTest(RequireTaskQueue("wat", f))(rec, &http.Request{ |
| 95 Header: http.Header{ |
| 96 http.CanonicalHeaderKey("x-appengine-que
uename"): []string{"wat"}, |
| 97 }, |
| 98 }, nil) |
| 99 So(hit, ShouldBeTrue) |
| 100 So(rec.Body.String(), ShouldEqual, "ok") |
| 101 So(rec.Code, ShouldEqual, http.StatusOK) |
| 102 }) |
| 103 |
| 104 Convey("from any tq succeeds", func() { |
| 105 rec := httptest.NewRecorder() |
| 106 BaseTest(RequireTaskQueue("", f))(rec, &http.Request{ |
| 107 Header: http.Header{ |
| 108 http.CanonicalHeaderKey("x-appengine-que
uename"): []string{"wat"}, |
| 109 }, |
| 110 }, nil) |
| 111 So(hit, ShouldBeTrue) |
| 112 So(rec.Body.String(), ShouldEqual, "ok") |
| 113 So(rec.Code, ShouldEqual, http.StatusOK) |
| 114 }) |
| 115 }) |
| 116 } |
OLD | NEW |