| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. | 1 // Copyright 2017 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 88 |
| 89 serverErr := <-done | 89 serverErr := <-done |
| 90 So(serverErr, ShouldBeNil) | 90 So(serverErr, ShouldBeNil) |
| 91 }) | 91 }) |
| 92 } | 92 } |
| 93 | 93 |
| 94 func TestProtocol(t *testing.T) { | 94 func TestProtocol(t *testing.T) { |
| 95 t.Parallel() | 95 t.Parallel() |
| 96 | 96 |
| 97 ctx := context.Background() | 97 ctx := context.Background() |
| 98 » ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeLocal) | 98 » ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) |
| 99 | 99 |
| 100 Convey("With server", t, func(c C) { | 100 Convey("With server", t, func(c C) { |
| 101 // Use channels to pass mocked requests/responses back and forth
. | 101 // Use channels to pass mocked requests/responses back and forth
. |
| 102 requests := make(chan []string, 10000) | 102 requests := make(chan []string, 10000) |
| 103 responses := make(chan interface{}, 1) | 103 responses := make(chan interface{}, 1) |
| 104 | 104 |
| 105 testGen := func(ctx context.Context, scopes []string, lifetime t
ime.Duration) (*oauth2.Token, error) { | 105 testGen := func(ctx context.Context, scopes []string, lifetime t
ime.Duration) (*oauth2.Token, error) { |
| 106 requests <- scopes | 106 requests <- scopes |
| 107 var resp interface{} | 107 var resp interface{} |
| 108 select { | 108 select { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 "secret": p.Secret, | 150 "secret": p.Secret, |
| 151 "account_id": "acc_id", | 151 "account_id": "acc_id", |
| 152 }) | 152 }) |
| 153 } | 153 } |
| 154 | 154 |
| 155 Convey("Happy path", func() { | 155 Convey("Happy path", func() { |
| 156 responses <- &oauth2.Token{ | 156 responses <- &oauth2.Token{ |
| 157 AccessToken: "tok1", | 157 AccessToken: "tok1", |
| 158 Expiry: clock.Now(ctx).Add(30 * time.Minute
), | 158 Expiry: clock.Now(ctx).Add(30 * time.Minute
), |
| 159 } | 159 } |
| 160 » » » So(call(goodRequest()), ShouldEqual, `HTTP 200 (json): {
"access_token":"tok1","expiry":1454502906}`) | 160 » » » So(call(goodRequest()), ShouldEqual, `HTTP 200 (json): {
"access_token":"tok1","expiry":1454474106}`) |
| 161 So(<-requests, ShouldResemble, []string{"A", "B"}) | 161 So(<-requests, ShouldResemble, []string{"A", "B"}) |
| 162 | 162 |
| 163 // application/json is also the default. | 163 // application/json is also the default. |
| 164 req := goodRequest() | 164 req := goodRequest() |
| 165 req.Header.Del("Content-Type") | 165 req.Header.Del("Content-Type") |
| 166 responses <- &oauth2.Token{ | 166 responses <- &oauth2.Token{ |
| 167 AccessToken: "tok2", | 167 AccessToken: "tok2", |
| 168 Expiry: clock.Now(ctx).Add(30 * time.Minute
), | 168 Expiry: clock.Now(ctx).Add(30 * time.Minute
), |
| 169 } | 169 } |
| 170 » » » So(call(req), ShouldEqual, `HTTP 200 (json): {"access_to
ken":"tok2","expiry":1454502906}`) | 170 » » » So(call(req), ShouldEqual, `HTTP 200 (json): {"access_to
ken":"tok2","expiry":1454474106}`) |
| 171 So(<-requests, ShouldResemble, []string{"A", "B"}) | 171 So(<-requests, ShouldResemble, []string{"A", "B"}) |
| 172 }) | 172 }) |
| 173 | 173 |
| 174 Convey("Panic in token generator", func() { | 174 Convey("Panic in token generator", func() { |
| 175 responses <- "omg, panic" | 175 responses <- "omg, panic" |
| 176 So(call(goodRequest()), ShouldEqual, `HTTP 500: Internal
Server Error. See logs.`) | 176 So(call(goodRequest()), ShouldEqual, `HTTP 500: Internal
Server Error. See logs.`) |
| 177 }) | 177 }) |
| 178 | 178 |
| 179 Convey("Not POST", func() { | 179 Convey("Not POST", func() { |
| 180 req := goodRequest() | 180 req := goodRequest() |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 panic(err) | 325 panic(err) |
| 326 } | 326 } |
| 327 | 327 |
| 328 tp := "" | 328 tp := "" |
| 329 if resp.Header.Get("Content-Type") == "application/json; charset=utf-8"
{ | 329 if resp.Header.Get("Content-Type") == "application/json; charset=utf-8"
{ |
| 330 tp = " (json)" | 330 tp = " (json)" |
| 331 } | 331 } |
| 332 | 332 |
| 333 return fmt.Sprintf("HTTP %d%s: %s", resp.StatusCode, tp, strings.TrimSpa
ce(string(blob))) | 333 return fmt.Sprintf("HTTP %d%s: %s", resp.StatusCode, tp, strings.TrimSpa
ce(string(blob))) |
| 334 } | 334 } |
| OLD | NEW |