| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package gethosts |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"fmt" |
| 22 |
"net/http" |
| 23 |
"net/http/httptest" |
| 24 |
"testing" |
| 25 |
"time" |
| 26 |
|
| 27 |
. "gopkg.in/check.v1" |
| 28 |
|
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/external/murmur3" |
| 30 |
) |
| 31 |
|
| 32 |
func TestGetHosts(t *testing.T) { TestingT(t) } |
| 33 |
|
| 34 |
type getHostsSuite struct{} |
| 35 |
|
| 36 |
var _ = Suite(&getHostsSuite{}) |
| 37 |
|
| 38 |
func (s *getHostsSuite) TestNew(c *C) { |
| 39 |
gh := New("foobar", "http://where/hosts", 10*time.Second) |
| 40 |
c.Check(gh.hash, Equals, fmt.Sprintf("%x", murmur3.Sum64([]byte("foobar")))) |
| 41 |
c.Check(gh.endpointUrl, Equals, "http://where/hosts") |
| 42 |
} |
| 43 |
|
| 44 |
func (s *getHostsSuite) TestGet(c *C) { |
| 45 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 46 |
x := r.FormValue("h") |
| 47 |
b, err := json.Marshal(map[string]interface{}{ |
| 48 |
"domain": "example.com", |
| 49 |
"hosts": []string{"http://" + x}, |
| 50 |
}) |
| 51 |
if err != nil { |
| 52 |
panic(err) |
| 53 |
} |
| 54 |
w.Header().Set("Content-Type", "application/json") |
| 55 |
w.Write(b) |
| 56 |
})) |
| 57 |
defer ts.Close() |
| 58 |
gh := New("foobar", ts.URL, 1*time.Second) |
| 59 |
res, err := gh.Get() |
| 60 |
c.Assert(err, IsNil) |
| 61 |
c.Check(*res, DeepEquals, |
| 62 |
Host{Domain: "example.com", Hosts: []string{"http://bdd2ae7116c85a45"}}) |
| 63 |
} |
| 64 |
|
| 65 |
func (s *getHostsSuite) TestGetTimeout(c *C) { |
| 66 |
started := make(chan bool, 1) |
| 67 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 |
started <- true |
| 69 |
time.Sleep(700 * time.Millisecond) |
| 70 |
})) |
| 71 |
defer func() { |
| 72 |
<-started |
| 73 |
ts.Close() |
| 74 |
}() |
| 75 |
gh := New("foobar", ts.URL, 500*time.Millisecond) |
| 76 |
_, err := gh.Get() |
| 77 |
c.Check(err, ErrorMatches, ".*Timeout.*") |
| 78 |
} |
| 79 |
|
| 80 |
func (s *getHostsSuite) TestGetErrorScenarios(c *C) { |
| 81 |
status := make(chan int, 1) |
| 82 |
body := make(chan string, 1) |
| 83 |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 84 |
w.WriteHeader(<-status) |
| 85 |
fmt.Fprintf(w, "%s", <-body) |
| 86 |
})) |
| 87 |
defer ts.Close() |
| 88 |
gh := New("foobar", ts.URL, 1*time.Second) |
| 89 |
scenario := func(st int, bdy string, expectedErr error) { |
| 90 |
status <- st |
| 91 |
body <- bdy |
| 92 |
_, err := gh.Get() |
| 93 |
c.Check(err, Equals, expectedErr) |
| 94 |
} |
| 95 |
|
| 96 |
scenario(http.StatusBadRequest, "", ErrRequest) |
| 97 |
scenario(http.StatusInternalServerError, "", ErrInternal) |
| 98 |
scenario(http.StatusGatewayTimeout, "", ErrTemporary) |
| 99 |
|
| 100 |
scenario(http.StatusOK, "{", ErrTemporary) |
| 101 |
scenario(http.StatusOK, "{}", ErrTemporary) |
| 102 |
scenario(http.StatusOK, `{"domain": "example.com"}`, ErrTemporary) |
| 103 |
scenario(http.StatusOK, `{"hosts": ["one"]}`, nil) |
| 104 |
} |