Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: go/src/infra/gae/libs/ephelper/ephelper_test.go

Issue 1153473008: A client/server helper wrapper for endpoints in Go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: rename to better package Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 ephelper
6
7 import (
8 "net/http"
9 "testing"
10
11 "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
12 . "github.com/smartystreets/goconvey/convey"
13 )
14
15 type FakeExportedServerBadMethods struct{}
16
17 func (FakeExportedServerBadMethods) fooMethod() {}
18
19 type FakeExportedServer struct{}
20
21 func (FakeExportedServer) FooMethod(*http.Request) error {
22 return nil
23 }
24
25 func TestRegister(t *testing.T) {
26 t.Parallel()
27
28 Convey("Testing Register(...)", t, func() {
29
30 Convey("errors", func() {
31 Convey("nil server", func() {
32 err := Register(nil, nil, nil, nil)
33 So(err, ShouldEqual, ErrServerNil)
34 })
35 Convey("nil service", func() {
36 serv := endpoints.NewServer("")
37 err := Register(serv, nil, nil, nil)
38 So(err, ShouldEqual, ErrServiceNil)
39 })
40 Convey("registration error", func() {
41 s := FakeExportedServerBadMethods{}
42 si := &endpoints.ServiceInfo{}
43 mi := map[string]*endpoints.MethodInfo{
44 "fooMethod": {},
45 }
46 serv := endpoints.NewServer("")
47 err := Register(serv, s, si, mi)
48 So(err.Error(), ShouldContainSubstring,
49 "no exported methods")
50 })
51 Convey("method mismatch", func() {
52 s := FakeExportedServer{}
53 si := &endpoints.ServiceInfo{}
54 mi := map[string]*endpoints.MethodInfo{
55 "fooMethod": {},
56 }
57 serv := endpoints.NewServer("")
58 err := Register(serv, s, si, mi)
59 So(err.Error(), ShouldContainSubstring,
60 "no method \"fooMethod\"")
61 })
62 })
63
64 Convey("Success", func() {
65 Convey("defaults", func() {
66 s := FakeExportedServer{}
67 srv := endpoints.NewServer("")
68 err := Register(srv, s, nil, nil)
69 So(err, ShouldBeNil)
70 fakeServer := srv.ServiceByName("FakeExportedSer ver")
71 So(fakeServer, ShouldNotBeNil)
72 So(fakeServer.Info().Name, ShouldEqual, "fakeexp ortedserver")
73 So(fakeServer.Info().Version, ShouldEqual, "v1")
74 So(fakeServer.Info().Default, ShouldBeTrue)
75 So(fakeServer.Info().Description, ShouldEqual, " ")
76
77 m := fakeServer.MethodByName("FooMethod")
78 So(m, ShouldNotBeNil)
79 So(m.Info().Name, ShouldEqual, "foomethod")
80 So(m.Info().Path, ShouldEqual, "foomethod")
81 So(m.Info().HTTPMethod, ShouldEqual, "GET")
82 So(m.Info().Desc, ShouldEqual, "")
83 })
84
85 Convey("overrides", func() {
86 s := FakeExportedServer{}
87 si := &endpoints.ServiceInfo{
88 Name: "fakeThing",
89 Version: "9001",
90 Description: "A space oddysey.",
91 }
92 mi := MethodInfoMap{
93 "FooMethod": {
94 Name: "foo_method",
95 Path: "foo",
96 HTTPMethod: "POST",
97 Desc: "does useful stuff",
98 },
99 }
100 srv := endpoints.NewServer("")
101 err := Register(srv, s, si, mi)
102 So(err, ShouldBeNil)
103 fakeServer := srv.ServiceByName("FakeExportedSer ver")
104 So(fakeServer, ShouldNotBeNil)
105 So(fakeServer.Info().Name, ShouldEqual, "fakethi ng")
106 So(fakeServer.Info().Version, ShouldEqual, "9001 ")
107 So(fakeServer.Info().Default, ShouldBeFalse)
108 So(fakeServer.Info().Description, ShouldEqual, " A space oddysey.")
109
110 m := fakeServer.MethodByName("FooMethod")
111 So(m, ShouldNotBeNil)
112 So(m.Info().Name, ShouldEqual, "foo_method")
113 So(m.Info().Path, ShouldEqual, "foo")
114 So(m.Info().HTTPMethod, ShouldEqual, "POST")
115 So(m.Info().Desc, ShouldEqual, "does useful stuf f")
116 })
117
118 Convey("merge", func() {
119 s := FakeExportedServer{}
120 mi := MethodInfoMap{
121 "FooMethod": {
122 Desc: "does useful stuff",
123 },
124 }
125 srv := endpoints.NewServer("")
126 err := Register(srv, s, nil, mi)
127 So(err, ShouldBeNil)
128 fakeServer := srv.ServiceByName("FakeExportedSer ver")
129 m := fakeServer.MethodByName("FooMethod")
130 So(m, ShouldNotBeNil)
131 So(m.Info().Name, ShouldEqual, "foomethod")
132 So(m.Info().Path, ShouldEqual, "foomethod")
133 So(m.Info().HTTPMethod, ShouldEqual, "GET")
134 So(m.Info().Desc, ShouldEqual, "does useful stuf f")
135 })
136 })
137 })
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698