...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/auth/mongodbaws_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/auth

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package auth
     8  
     9  import (
    10  	"errors"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/internal/assert"
    14  )
    15  
    16  func TestGetRegion(t *testing.T) {
    17  	longHost := make([]rune, 256)
    18  	emptyErr := errors.New("invalid STS host: empty")
    19  	tooLongErr := errors.New("invalid STS host: too large")
    20  	emptyPartErr := errors.New("invalid STS host: empty part")
    21  	testCases := []struct {
    22  		name   string
    23  		host   string
    24  		err    error
    25  		region string
    26  	}{
    27  		{"success default", "sts.amazonaws.com", nil, "us-east-1"},
    28  		{"success parse", "first.second", nil, "second"},
    29  		{"success no region", "first", nil, "us-east-1"},
    30  		{"error host too long", string(longHost), tooLongErr, ""},
    31  		{"error host empty", "", emptyErr, ""},
    32  		{"error empty middle part", "abc..def", emptyPartErr, ""},
    33  		{"error empty part", "first.", emptyPartErr, ""},
    34  	}
    35  	for _, tc := range testCases {
    36  		t.Run(tc.name, func(t *testing.T) {
    37  			reg, err := getRegion(tc.host)
    38  			if tc.err == nil {
    39  				assert.Nil(t, err, "error getting region: %v", err)
    40  				assert.Equal(t, tc.region, reg, "expected %v, got %v", tc.region, reg)
    41  				return
    42  			}
    43  			assert.NotNil(t, err, "expected error, got nil")
    44  			assert.Equal(t, err, tc.err, "expected error: %v, got: %v", tc.err, err)
    45  		})
    46  	}
    47  
    48  }
    49  

View as plain text