github.com/icyphox/x@v0.0.355-0.20220311094250-029bd783e8b8/metricsx/middleware_test.go (about)

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  
    21  package metricsx
    22  
    23  import (
    24  	"net/url"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestAnonymizePath(t *testing.T) {
    31  	m := &Service{
    32  		o: &Options{WhitelistedPaths: []string{"/keys"}},
    33  	}
    34  
    35  	assert.Equal(t, "/keys/837b4168b57215f2ba0d4e64e57a653d6a6ecd6065e78598283209467d172373", m.anonymizePath("/keys/1234", "somesupersaltysalt"))
    36  	assert.Equal(t, "/keys", m.anonymizePath("/keys", "somesupersaltysalt"))
    37  }
    38  
    39  func TestAnonymizeQuery(t *testing.T) {
    40  	m := &Service{}
    41  
    42  	assert.EqualValues(t, "foo=2ec879270efe890972d975251e9d454f4af49df1f07b4317fd5b6ae90de4c774&foo=1864a573566eba1b9ddab79d8f4bab5a39c938918a21b80a64ae1c9c12fa9aa2&foo2=186084f6bd8e222bedade9439d6ae69ed274b954eeebe9b54fd5f47e54dd7675&foo2=1ee7158281cc3b5a27de4c337e07987e8677f5f687a4671ca369b79c653d379d", m.anonymizeQuery(url.Values{
    43  		"foo":  []string{"bar", "baz"},
    44  		"foo2": []string{"bar2", "baz2"},
    45  	}, "somesupersaltysalt"))
    46  	assert.EqualValues(t, "", m.anonymizeQuery(url.Values{
    47  		"foo": []string{},
    48  	}, "somesupersaltysalt"))
    49  	assert.EqualValues(t, "foo=", m.anonymizeQuery(url.Values{
    50  		"foo": []string{""},
    51  	}, "somesupersaltysalt"))
    52  	assert.EqualValues(t, "", m.anonymizeQuery(url.Values{}, "somesupersaltysalt"))
    53  }