Debian Codegraph @

plugins/gnostic-go-generator/examples/v2.0/sample/sample_test.go

raw symbols SCIP
1 /*
2 Copyright 2018 Google Inc. All Rights Reserved.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package test
18
19 import (
20 "fmt"
21 "net/http"
22 "strings"
23 "testing"
24
25 "github.com/googleapis/gnostic/plugins/gnostic-go-generator/examples/v2.0/sample/sample"
26 )
27
28 const service = "http://localhost:8080"
29
30 func TestSample(t *testing.T) {
31 // create a client
32 s := sample.NewClient(service, nil)
33 // verify a sample request
34 {
35 message := "hello world"
36 response, err := s.GetSample(message)
37 if err != nil {
38 t.Log("get sample failed")
39 t.Fail()
40 }
41 if response.OK.Id != message || response.OK.Count != int32(len(message)) {
42 t.Log(fmt.Sprintf("get sample received %+v", response.OK))
43 t.Fail()
44 }
45 if (response == nil) || (response.OK == nil) {
46 t.Log(fmt.Sprintf("get sample failed %+v", response.OK))
47 t.Fail()
48 }
49 }
50 // verify the handling of an invalid request
51 {
52 req, err := http.NewRequest("GET", service+"/unsupported", strings.NewReader(""))
53 if err != nil {
54 t.Log("bad request failed")
55 return
56 }
57 resp, err := http.DefaultClient.Do(req)
58 if err != nil {
59 return
60 }
61 // we expect a 404 (Not Found) code
62 if resp.StatusCode != 404 {
63 t.Log("bad request failed")
64 t.Fail()
65 }
66 return
67 }
68 }