github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/resolver/subdomain/subdomain_test.go (about)

     1  // Copyright 2020 Asim Aslam
     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  //     https://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  // Original source: github.com/micro/go-micro/v3/api/resolver/subdomain/subdomain_test.go
    16  
    17  package subdomain
    18  
    19  import (
    20  	"net/http"
    21  	"net/url"
    22  	"testing"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/service/api/resolver/vpath"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestResolve(t *testing.T) {
    30  	tt := []struct {
    31  		Name   string
    32  		Host   string
    33  		Result string
    34  	}{
    35  		{
    36  			Name:   "Top level domain",
    37  			Host:   "micro.mu",
    38  			Result: "micro",
    39  		},
    40  		{
    41  			Name:   "Effective top level domain",
    42  			Host:   "micro.com.au",
    43  			Result: "micro",
    44  		},
    45  		{
    46  			Name:   "Subdomain dev",
    47  			Host:   "dev.micro.mu",
    48  			Result: "dev",
    49  		},
    50  		{
    51  			Name:   "Subdomain foo",
    52  			Host:   "foo.micro.mu",
    53  			Result: "foo",
    54  		},
    55  		{
    56  			Name:   "Multi-level subdomain",
    57  			Host:   "staging.myapp.m3o.app",
    58  			Result: "myapp-staging",
    59  		},
    60  		{
    61  			Name:   "Dev host",
    62  			Host:   "127.0.0.1",
    63  			Result: "micro",
    64  		},
    65  		{
    66  			Name:   "Localhost",
    67  			Host:   "localhost",
    68  			Result: "micro",
    69  		},
    70  		{
    71  			Name:   "IP host",
    72  			Host:   "81.151.101.146",
    73  			Result: "micro",
    74  		},
    75  	}
    76  
    77  	for _, tc := range tt {
    78  		t.Run(tc.Name, func(t *testing.T) {
    79  			r := NewResolver(vpath.NewResolver())
    80  			result, err := r.Resolve(&http.Request{URL: &url.URL{Host: tc.Host, Path: "foo/bar"}})
    81  			assert.Nil(t, err, "Expecter err to be nil")
    82  			if result != nil {
    83  				assert.Equal(t, tc.Result, result.Domain, "Expected %v but got %v", tc.Result, result.Domain)
    84  			}
    85  		})
    86  	}
    87  }