github.com/cs3org/reva/v2@v2.27.7/pkg/ctx/agentctx_test.go (about)

     1  // Copyright 2018-2021 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 ctx
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  
    25  	"google.golang.org/grpc/metadata"
    26  )
    27  
    28  func TestUserAgentIsAllowed(t *testing.T) {
    29  
    30  	tests := []struct {
    31  		description string
    32  		userAgent   string
    33  		expected    string
    34  	}{
    35  		{
    36  			description: "grpc-go",
    37  			userAgent:   "grpc-go",
    38  			expected:    "grpc",
    39  		},
    40  		{
    41  			description: "web-firefox",
    42  			userAgent:   "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15",
    43  			expected:    "web",
    44  		},
    45  		{
    46  			description: "desktop-mirall",
    47  			userAgent:   "Mozilla/5.0 (Linux) mirall/2.7.1 (build 2596) (cernboxcmd, centos-3.10.0-1160.36.2.el7.x86_64 ClientArchitecture: x86_64 OsArchitecture: x86_64)",
    48  			expected:    "desktop",
    49  		},
    50  		{
    51  			description: "mobile-android",
    52  			userAgent:   "Mozilla/5.0 (Android) ownCloud-android/2.13.1 cernbox/Android",
    53  			expected:    "mobile",
    54  		},
    55  		{
    56  			description: "mobile-ios",
    57  			userAgent:   "Mozilla/5.0 (iOS) ownCloud-iOS/3.8.0 cernbox/iOS",
    58  			expected:    "mobile",
    59  		},
    60  		{
    61  			description: "mobile-web",
    62  			userAgent:   "Mozilla/5.0 (Android 11; Mobile; rv:86.0) Gecko/86.0 Firefox/86.0",
    63  			expected:    "web",
    64  		},
    65  	}
    66  
    67  	ctx := context.Background()
    68  	for _, tt := range tests {
    69  		t.Run(tt.description, func(t *testing.T) {
    70  			ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{UserAgentHeader: tt.userAgent}))
    71  			cat, _ := ContextGetUserAgentCategory(ctx)
    72  
    73  			if cat != tt.expected {
    74  				t.Fatalf("result does not match with expected. got=%+v expected=%+v", cat, tt.expected)
    75  			}
    76  
    77  		})
    78  	}
    79  }