1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flight_test
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"reflect"
"strings"
"testing"
"time"
"github.com/apache/arrow-go/v18/arrow/flight"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
// strings.Cut is go1.18+ so let's just stick a duplicate of it in here
// for now since we want to support go1.17
func cut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}
type serverAddCookieMiddleware struct {
expectedCookies map[string]string
cookies []*http.Cookie
}
func (s *serverAddCookieMiddleware) StartCall(ctx context.Context) context.Context {
if s.expectedCookies == nil {
md := make(metadata.MD)
for _, c := range s.cookies {
md.Append("Set-Cookie", c.String())
}
grpc.SetHeader(ctx, md)
return nil
}
cookies := metadata.ValueFromIncomingContext(ctx, "cookie")
got := make(map[string]string)
for _, line := range cookies {
line = textproto.TrimString(line)
var part string
for len(line) > 0 {
part, line, _ = cut(line, ";")
part = textproto.TrimString(part)
if part == "" {
continue
}
name, val, _ := cut(part, "=")
name = textproto.TrimString(name)
if len(val) > 1 && val[0] == '"' && val[len(val)-1] == '"' {
val = val[1 : len(val)-1]
}
got[name] = val
}
}
if !reflect.DeepEqual(s.expectedCookies, got) {
panic(fmt.Sprintf("did not get expected cookies, expected %+v, got %+v", s.expectedCookies, got))
}
return nil
}
func (s *serverAddCookieMiddleware) CallCompleted(ctx context.Context, err error) {}
func TestClientCookieMiddleware(t *testing.T) {
cookieMiddleware := &serverAddCookieMiddleware{}
s := flight.NewServerWithMiddleware([]flight.ServerMiddleware{
flight.CreateServerMiddleware(cookieMiddleware),
})
s.Init("localhost:0")
f := &flightServer{}
s.RegisterFlightService(f)
go s.Serve()
defer s.Shutdown()
credsOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
tests := []struct {
testname string
cookies []*http.Cookie
expected map[string]string
}{
{"single cookie", []*http.Cookie{{Name: "Cookie-1", Value: "v$1", Raw: "Cookie-1=v$1"}},
map[string]string{"Cookie-1": "v$1"}},
{"expired", []*http.Cookie{{
Name: "NID", Value: "99=YsDT5", Expires: time.Date(2011, 11, 23, 1, 5, 3, 0, time.UTC),
RawExpires: "Wed, 23-Nov-2011 01:05:03 GMT", Raw: "NID=99=YsDT5; expires=Wed, 23-Nov-11 01:05:03 GMT"}},
map[string]string{}},
{"multiple", []*http.Cookie{
{Name: "negative maxage", Value: "foobar", MaxAge: -1},
{Name: "special-1", Value: " z"},
{Name: "cookie-2", Value: "v$2"},
},
map[string]string{"special-1": " z", "cookie-2": "v$2"}},
}
makeReq := func(c flight.Client, t *testing.T) {
flightStream, err := c.ListFlights(context.Background(), &flight.Criteria{})
assert.NoError(t, err)
for {
_, err := flightStream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
assert.NoError(t, err)
}
}
}
for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
cookieMiddleware.expectedCookies = nil
client, err := flight.NewClientWithMiddleware(s.Addr().String(), nil,
[]flight.ClientMiddleware{flight.NewClientCookieMiddleware()}, credsOpt)
require.NoError(t, err)
defer client.Close()
cookieMiddleware.cookies = tt.cookies
makeReq(client, t)
cookieMiddleware.expectedCookies = tt.expected
makeReq(client, t)
})
}
}
func TestCookieExpiration(t *testing.T) {
cookieMiddleware := &serverAddCookieMiddleware{}
s := flight.NewServerWithMiddleware([]flight.ServerMiddleware{
flight.CreateServerMiddleware(cookieMiddleware),
})
s.Init("localhost:0")
f := &flightServer{}
s.RegisterFlightService(f)
go s.Serve()
defer s.Shutdown()
makeReq := func(c flight.Client, t *testing.T) {
flightStream, err := c.ListFlights(context.Background(), &flight.Criteria{})
assert.NoError(t, err)
for {
_, err := flightStream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
assert.NoError(t, err)
}
}
}
credsOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
client, err := flight.NewClientWithMiddleware(s.Addr().String(), nil,
[]flight.ClientMiddleware{flight.NewClientCookieMiddleware()}, credsOpt)
require.NoError(t, err)
defer client.Close()
// set cookies
cookieMiddleware.cookies = []*http.Cookie{
{Name: "foo", Value: "bar"},
{Name: "foo2", Value: "bar2", MaxAge: 1},
}
makeReq(client, t)
// validate set
cookieMiddleware.expectedCookies = map[string]string{
"foo": "bar", "foo2": "bar2",
}
makeReq(client, t)
// wait for foo2 to expire and validate it doesn't get sent
time.Sleep(1 * time.Second)
cookieMiddleware.expectedCookies = map[string]string{
"foo": "bar",
}
makeReq(client, t)
// update value
cookieMiddleware.cookies = []*http.Cookie{
{Name: "foo", Value: "baz"},
}
cookieMiddleware.expectedCookies = nil
makeReq(client, t)
// validate updated value is sent
cookieMiddleware.expectedCookies = map[string]string{
"foo": "baz",
}
makeReq(client, t)
// force delete cookie
cookieMiddleware.expectedCookies = nil
cookieMiddleware.cookies = []*http.Cookie{
{Name: "foo", MaxAge: -1}, // delete now!
}
makeReq(client, t)
// verify it's been deleted
cookieMiddleware.expectedCookies = map[string]string{}
makeReq(client, t)
}
func TestCookiesClone(t *testing.T) {
cookieMiddleware := &serverAddCookieMiddleware{}
s := flight.NewServerWithMiddleware([]flight.ServerMiddleware{
flight.CreateServerMiddleware(cookieMiddleware),
})
s.Init("localhost:0")
f := &flightServer{}
s.RegisterFlightService(f)
go s.Serve()
defer s.Shutdown()
makeReq := func(c flight.Client, t *testing.T) {
flightStream, err := c.ListFlights(context.Background(), &flight.Criteria{})
assert.NoError(t, err)
for {
_, err := flightStream.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
break
}
assert.NoError(t, err)
}
}
}
credsOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
cookies := flight.NewCookieMiddleware()
client1, err := flight.NewClientWithMiddleware(s.Addr().String(), nil,
[]flight.ClientMiddleware{flight.CreateClientMiddleware(cookies)}, credsOpt)
require.NoError(t, err)
defer client1.Close()
// set cookies
cookieMiddleware.cookies = []*http.Cookie{
{Name: "foo", Value: "bar"},
{Name: "foo2", Value: "bar2", MaxAge: 1},
}
makeReq(client1, t)
// validate set
cookieMiddleware.expectedCookies = map[string]string{
"foo": "bar", "foo2": "bar2",
}
makeReq(client1, t)
client2, err := flight.NewClientWithMiddleware(s.Addr().String(), nil,
[]flight.ClientMiddleware{flight.CreateClientMiddleware(cookies.Clone())}, credsOpt)
require.NoError(t, err)
defer client2.Close()
// validate clone worked
cookieMiddleware.expectedCookies = map[string]string{
"foo": "bar", "foo2": "bar2",
}
makeReq(client2, t)
}
|