1// Copyright 2019 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (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 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Code generated by gapic-generator. DO NOT EDIT. 16 17package phishingprotection 18 19import ( 20 "context" 21 "flag" 22 "fmt" 23 "io" 24 "log" 25 "net" 26 "os" 27 "strings" 28 "testing" 29 30 "github.com/golang/protobuf/proto" 31 "github.com/golang/protobuf/ptypes" 32 "google.golang.org/api/option" 33 phishingprotectionpb "google.golang.org/genproto/googleapis/cloud/phishingprotection/v1beta1" 34 35 status "google.golang.org/genproto/googleapis/rpc/status" 36 "google.golang.org/grpc" 37 "google.golang.org/grpc/codes" 38 "google.golang.org/grpc/metadata" 39 40 gstatus "google.golang.org/grpc/status" 41) 42 43var _ = io.EOF 44var _ = ptypes.MarshalAny 45var _ status.Status 46 47type mockPhishingProtectionServiceV1Beta1Server struct { 48 // Embed for forward compatibility. 49 // Tests will keep working if more methods are added 50 // in the future. 51 phishingprotectionpb.PhishingProtectionServiceV1Beta1Server 52 53 reqs []proto.Message 54 55 // If set, all calls return this error. 56 err error 57 58 // responses to return if err == nil 59 resps []proto.Message 60} 61 62func (s *mockPhishingProtectionServiceV1Beta1Server) ReportPhishing(ctx context.Context, req *phishingprotectionpb.ReportPhishingRequest) (*phishingprotectionpb.ReportPhishingResponse, error) { 63 md, _ := metadata.FromIncomingContext(ctx) 64 if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") { 65 return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg) 66 } 67 s.reqs = append(s.reqs, req) 68 if s.err != nil { 69 return nil, s.err 70 } 71 return s.resps[0].(*phishingprotectionpb.ReportPhishingResponse), nil 72} 73 74// clientOpt is the option tests should use to connect to the test server. 75// It is initialized by TestMain. 76var clientOpt option.ClientOption 77 78var ( 79 mockPhishingProtectionServiceV1Beta1 mockPhishingProtectionServiceV1Beta1Server 80) 81 82func TestMain(m *testing.M) { 83 flag.Parse() 84 85 serv := grpc.NewServer() 86 phishingprotectionpb.RegisterPhishingProtectionServiceV1Beta1Server(serv, &mockPhishingProtectionServiceV1Beta1) 87 88 lis, err := net.Listen("tcp", "localhost:0") 89 if err != nil { 90 log.Fatal(err) 91 } 92 go serv.Serve(lis) 93 94 conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure()) 95 if err != nil { 96 log.Fatal(err) 97 } 98 clientOpt = option.WithGRPCConn(conn) 99 100 os.Exit(m.Run()) 101} 102 103func TestPhishingProtectionServiceV1Beta1ReportPhishing(t *testing.T) { 104 var expectedResponse *phishingprotectionpb.ReportPhishingResponse = &phishingprotectionpb.ReportPhishingResponse{} 105 106 mockPhishingProtectionServiceV1Beta1.err = nil 107 mockPhishingProtectionServiceV1Beta1.reqs = nil 108 109 mockPhishingProtectionServiceV1Beta1.resps = append(mockPhishingProtectionServiceV1Beta1.resps[:0], expectedResponse) 110 111 var formattedParent string = fmt.Sprintf("projects/%s", "[PROJECT]") 112 var uri string = "uri116076" 113 var request = &phishingprotectionpb.ReportPhishingRequest{ 114 Parent: formattedParent, 115 Uri: uri, 116 } 117 118 c, err := NewPhishingProtectionServiceV1Beta1Client(context.Background(), clientOpt) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 resp, err := c.ReportPhishing(context.Background(), request) 124 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 if want, got := request, mockPhishingProtectionServiceV1Beta1.reqs[0]; !proto.Equal(want, got) { 130 t.Errorf("wrong request %q, want %q", got, want) 131 } 132 133 if want, got := expectedResponse, resp; !proto.Equal(want, got) { 134 t.Errorf("wrong response %q, want %q)", got, want) 135 } 136} 137 138func TestPhishingProtectionServiceV1Beta1ReportPhishingError(t *testing.T) { 139 errCode := codes.PermissionDenied 140 mockPhishingProtectionServiceV1Beta1.err = gstatus.Error(errCode, "test error") 141 142 var formattedParent string = fmt.Sprintf("projects/%s", "[PROJECT]") 143 var uri string = "uri116076" 144 var request = &phishingprotectionpb.ReportPhishingRequest{ 145 Parent: formattedParent, 146 Uri: uri, 147 } 148 149 c, err := NewPhishingProtectionServiceV1Beta1Client(context.Background(), clientOpt) 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 resp, err := c.ReportPhishing(context.Background(), request) 155 156 if st, ok := gstatus.FromError(err); !ok { 157 t.Errorf("got error %v, expected grpc error", err) 158 } else if c := st.Code(); c != errCode { 159 t.Errorf("got error code %q, want %q", c, errCode) 160 } 161 _ = resp 162} 163