|
| 1 | +// Copyright 2018-2024 CERN |
| 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 | +// http://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 | +// In applying this license, CERN does not waive the privileges and immunities |
| 16 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 17 | +// or submit itself to any jurisdiction. |
| 18 | + |
| 19 | +package ocdav |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + |
| 29 | + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" |
| 30 | + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" |
| 31 | + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 32 | + mockgateway "github.com/cs3org/go-cs3apis/mocks/github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" |
| 33 | + "github.com/cs3org/reva/pkg/httpclient" |
| 34 | + "github.com/cs3org/reva/pkg/rgrpc/todo/pool" |
| 35 | + "github.com/rs/zerolog" |
| 36 | + "github.com/stretchr/testify/mock" |
| 37 | +) |
| 38 | + |
| 39 | +// Test that when calls come in to the PUT endpoint with a X-Disable-Versioning header, |
| 40 | +// this header is propagated to the actual upload endpoint |
| 41 | +func TestDisableVersioningHeaderPassedAlong(t *testing.T) { |
| 42 | + |
| 43 | + gatewayAPIEndpoint := "my-api-endpoint" |
| 44 | + incomingPath := "http://my-reva.com/myfile.txt" |
| 45 | + input := "Hello world!" |
| 46 | + |
| 47 | + // create HTTP request |
| 48 | + request := httptest.NewRequest(http.MethodPut, incomingPath, strings.NewReader(input)) |
| 49 | + request.Header.Add(HeaderContentLength, strconv.Itoa(len(input))) |
| 50 | + request.Header.Add(HeaderDisableVersioning, "true") |
| 51 | + |
| 52 | + // Create fake HTTP server for upload endpoint |
| 53 | + // Here we will check whether the header was correctly set |
| 54 | + calls := 0 |
| 55 | + w := httptest.NewRecorder() |
| 56 | + mockServerUpload := httptest.NewServer( |
| 57 | + http.HandlerFunc( |
| 58 | + func(w http.ResponseWriter, r *http.Request) { |
| 59 | + if header := r.Header.Get(HeaderDisableVersioning); header == "" { |
| 60 | + t.Errorf("expected header %s but header was not set", HeaderDisableVersioning) |
| 61 | + } |
| 62 | + calls++ |
| 63 | + }, |
| 64 | + ), |
| 65 | + ) |
| 66 | + endpointPath := mockServerUpload.URL |
| 67 | + |
| 68 | + // Set up mocked GatewayAPIClient |
| 69 | + gatewayClient := mockgateway.NewMockGatewayAPIClient(t) |
| 70 | + gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{Status: &rpc.Status{Code: rpc.Code_CODE_NOT_FOUND}}, nil) |
| 71 | + gatewayClient.On("InitiateFileUpload", mock.Anything, mock.Anything).Return(&gateway.InitiateFileUploadResponse{ |
| 72 | + Status: &rpc.Status{Code: rpc.Code_CODE_OK}, |
| 73 | + Protocols: []*gateway.FileUploadProtocol{ |
| 74 | + {Protocol: "simple", UploadEndpoint: endpointPath, Token: "my-secret-token"}, |
| 75 | + }}, nil) |
| 76 | + pool.RegisterGatewayServiceClient(gatewayClient, gatewayAPIEndpoint) |
| 77 | + |
| 78 | + // Set up OCDAV Service |
| 79 | + service := svc{ |
| 80 | + c: &Config{ |
| 81 | + GatewaySvc: gatewayAPIEndpoint, |
| 82 | + }, |
| 83 | + client: httpclient.New(), |
| 84 | + } |
| 85 | + ref := provider.Reference{} |
| 86 | + |
| 87 | + // Do the actual call |
| 88 | + service.handlePut(context.Background(), w, request, &ref, zerolog.Logger{}) |
| 89 | + |
| 90 | + // If no connection was made to the upload endpoint, something is also wrong |
| 91 | + if calls == 0 { |
| 92 | + t.Errorf("Upload endpoint was not called. ") |
| 93 | + } |
| 94 | +} |
0 commit comments