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 epfrontend | |
6 | |
7 import ( | |
8 "encoding/json" | |
9 "fmt" | |
10 "io/ioutil" | |
11 "net/url" | |
12 "path/filepath" | |
13 "testing" | |
14 | |
15 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
16 . "github.com/smartystreets/goconvey/convey" | |
17 ) | |
18 | |
19 type discoveryTestCase struct { | |
20 backend endpoints.APIDescriptor | |
21 frontend restDescription | |
22 } | |
23 | |
24 func loadJSONTestCase(d interface{}, suite, name, kind string) { | |
25 path := filepath.Join(fmt.Sprintf("%s_testdata", suite), fmt.Sprintf("%s
_%s.json", name, kind)) | |
26 data, err := ioutil.ReadFile(path) | |
27 if err != nil { | |
28 panic(fmt.Errorf("failed to load test data [%s]: %v", path, err)
) | |
29 } | |
30 | |
31 if err := json.Unmarshal(data, d); err != nil { | |
32 panic(fmt.Errorf("failed to unmarshal [%s]: %v", path, err)) | |
33 } | |
34 } | |
35 | |
36 func loadDiscoveryTestCase(name string) *discoveryTestCase { | |
37 tc := discoveryTestCase{} | |
38 loadJSONTestCase(&tc.backend, "discovery", name, "backend") | |
39 loadJSONTestCase(&tc.frontend, "discovery", name, "frontend") | |
40 return &tc | |
41 } | |
42 | |
43 func TestBuildRestDescription(t *testing.T) { | |
44 Convey(`A testing APIDescriptor`, t, func() { | |
45 u, err := url.Parse("https://example.com/testing/v1") | |
46 So(err, ShouldBeNil) | |
47 | |
48 for _, tcName := range []string{ | |
49 "basic", | |
50 "query_get", | |
51 } { | |
52 Convey(fmt.Sprintf(`Correctly loads/generates the %q tes
t case.`, tcName), func() { | |
53 | |
54 tc := loadDiscoveryTestCase(tcName) | |
55 rd, err := buildRestDescription(u, &tc.backend) | |
56 So(err, ShouldBeNil) | |
57 So(rd, ShouldResemble, &tc.frontend) | |
58 }) | |
59 } | |
60 }) | |
61 } | |
OLD | NEW |