github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/ghinstance/host_test.go (about)

     1  package ghinstance
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestIsEnterprise(t *testing.T) {
    10  	tests := []struct {
    11  		host string
    12  		want bool
    13  	}{
    14  		{
    15  			host: "github.com",
    16  			want: false,
    17  		},
    18  		{
    19  			host: "api.github.com",
    20  			want: false,
    21  		},
    22  		{
    23  			host: "github.localhost",
    24  			want: false,
    25  		},
    26  		{
    27  			host: "api.github.localhost",
    28  			want: false,
    29  		},
    30  		{
    31  			host: "garage.github.com",
    32  			want: false,
    33  		},
    34  		{
    35  			host: "ghe.io",
    36  			want: true,
    37  		},
    38  		{
    39  			host: "example.com",
    40  			want: true,
    41  		},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.host, func(t *testing.T) {
    45  			if got := IsEnterprise(tt.host); got != tt.want {
    46  				t.Errorf("IsEnterprise() = %v, want %v", got, tt.want)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestNormalizeHostname(t *testing.T) {
    53  	tests := []struct {
    54  		host string
    55  		want string
    56  	}{
    57  		{
    58  			host: "GitHub.com",
    59  			want: "github.com",
    60  		},
    61  		{
    62  			host: "api.github.com",
    63  			want: "github.com",
    64  		},
    65  		{
    66  			host: "ssh.github.com",
    67  			want: "github.com",
    68  		},
    69  		{
    70  			host: "upload.github.com",
    71  			want: "github.com",
    72  		},
    73  		{
    74  			host: "GitHub.localhost",
    75  			want: "github.localhost",
    76  		},
    77  		{
    78  			host: "api.github.localhost",
    79  			want: "github.localhost",
    80  		},
    81  		{
    82  			host: "garage.github.com",
    83  			want: "github.com",
    84  		},
    85  		{
    86  			host: "GHE.IO",
    87  			want: "ghe.io",
    88  		},
    89  		{
    90  			host: "git.my.org",
    91  			want: "git.my.org",
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.host, func(t *testing.T) {
    96  			if got := NormalizeHostname(tt.host); got != tt.want {
    97  				t.Errorf("NormalizeHostname() = %v, want %v", got, tt.want)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestHostnameValidator(t *testing.T) {
   104  	tests := []struct {
   105  		name     string
   106  		input    string
   107  		wantsErr bool
   108  	}{
   109  		{
   110  			name:     "valid hostname",
   111  			input:    "internal.instance",
   112  			wantsErr: false,
   113  		},
   114  		{
   115  			name:     "hostname with slashes",
   116  			input:    "//internal.instance",
   117  			wantsErr: true,
   118  		},
   119  		{
   120  			name:     "empty hostname",
   121  			input:    "   ",
   122  			wantsErr: true,
   123  		},
   124  		{
   125  			name:     "hostname with colon",
   126  			input:    "internal.instance:2205",
   127  			wantsErr: true,
   128  		},
   129  	}
   130  
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			err := HostnameValidator(tt.input)
   134  			if tt.wantsErr {
   135  				assert.Error(t, err)
   136  				return
   137  			}
   138  			assert.NoError(t, err)
   139  		})
   140  	}
   141  }
   142  func TestGraphQLEndpoint(t *testing.T) {
   143  	tests := []struct {
   144  		host string
   145  		want string
   146  	}{
   147  		{
   148  			host: "github.com",
   149  			want: "https://api.github.com/graphql",
   150  		},
   151  		{
   152  			host: "github.localhost",
   153  			want: "http://api.github.localhost/graphql",
   154  		},
   155  		{
   156  			host: "garage.github.com",
   157  			want: "https://garage.github.com/api/graphql",
   158  		},
   159  		{
   160  			host: "ghe.io",
   161  			want: "https://ghe.io/api/graphql",
   162  		},
   163  	}
   164  	for _, tt := range tests {
   165  		t.Run(tt.host, func(t *testing.T) {
   166  			if got := GraphQLEndpoint(tt.host); got != tt.want {
   167  				t.Errorf("GraphQLEndpoint() = %v, want %v", got, tt.want)
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  func TestRESTPrefix(t *testing.T) {
   174  	tests := []struct {
   175  		host string
   176  		want string
   177  	}{
   178  		{
   179  			host: "github.com",
   180  			want: "https://api.github.com/",
   181  		},
   182  		{
   183  			host: "github.localhost",
   184  			want: "http://api.github.localhost/",
   185  		},
   186  		{
   187  			host: "garage.github.com",
   188  			want: "https://garage.github.com/api/v3/",
   189  		},
   190  		{
   191  			host: "ghe.io",
   192  			want: "https://ghe.io/api/v3/",
   193  		},
   194  	}
   195  	for _, tt := range tests {
   196  		t.Run(tt.host, func(t *testing.T) {
   197  			if got := RESTPrefix(tt.host); got != tt.want {
   198  				t.Errorf("RESTPrefix() = %v, want %v", got, tt.want)
   199  			}
   200  		})
   201  	}
   202  }