github.com/secman-team/gh-api@v1.8.2/core/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: "ghe.io",
    24  			want: true,
    25  		},
    26  		{
    27  			host: "example.com",
    28  			want: true,
    29  		},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.host, func(t *testing.T) {
    33  			if got := IsEnterprise(tt.host); got != tt.want {
    34  				t.Errorf("IsEnterprise() = %v, want %v", got, tt.want)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestNormalizeHostname(t *testing.T) {
    41  	tests := []struct {
    42  		host string
    43  		want string
    44  	}{
    45  		{
    46  			host: "GitHub.com",
    47  			want: "github.com",
    48  		},
    49  		{
    50  			host: "api.github.com",
    51  			want: "github.com",
    52  		},
    53  		{
    54  			host: "ssh.github.com",
    55  			want: "github.com",
    56  		},
    57  		{
    58  			host: "upload.github.com",
    59  			want: "github.com",
    60  		},
    61  		{
    62  			host: "GHE.IO",
    63  			want: "ghe.io",
    64  		},
    65  		{
    66  			host: "git.my.org",
    67  			want: "git.my.org",
    68  		},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.host, func(t *testing.T) {
    72  			if got := NormalizeHostname(tt.host); got != tt.want {
    73  				t.Errorf("NormalizeHostname() = %v, want %v", got, tt.want)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestHostnameValidator(t *testing.T) {
    80  	tests := []struct {
    81  		name     string
    82  		input    interface{}
    83  		wantsErr bool
    84  	}{
    85  		{
    86  			name:     "valid hostname",
    87  			input:    "internal.instance",
    88  			wantsErr: false,
    89  		},
    90  		{
    91  			name:     "hostname with slashes",
    92  			input:    "//internal.instance",
    93  			wantsErr: true,
    94  		},
    95  		{
    96  			name:     "empty hostname",
    97  			input:    "   ",
    98  			wantsErr: true,
    99  		},
   100  		{
   101  			name:     "hostname with colon",
   102  			input:    "internal.instance:2205",
   103  			wantsErr: true,
   104  		},
   105  		{
   106  			name:     "non-string hostname",
   107  			input:    62,
   108  			wantsErr: true,
   109  		},
   110  	}
   111  
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			err := HostnameValidator(tt.input)
   115  			if tt.wantsErr {
   116  				assert.Error(t, err)
   117  				return
   118  			}
   119  			assert.NoError(t, err)
   120  		})
   121  	}
   122  }
   123  func TestGraphQLEndpoint(t *testing.T) {
   124  	tests := []struct {
   125  		host string
   126  		want string
   127  	}{
   128  		{
   129  			host: "github.com",
   130  			want: "https://api.github.com/graphql",
   131  		},
   132  		{
   133  			host: "ghe.io",
   134  			want: "https://ghe.io/api/graphql",
   135  		},
   136  	}
   137  	for _, tt := range tests {
   138  		t.Run(tt.host, func(t *testing.T) {
   139  			if got := GraphQLEndpoint(tt.host); got != tt.want {
   140  				t.Errorf("GraphQLEndpoint() = %v, want %v", got, tt.want)
   141  			}
   142  		})
   143  	}
   144  }
   145  
   146  func TestRESTPrefix(t *testing.T) {
   147  	tests := []struct {
   148  		host string
   149  		want string
   150  	}{
   151  		{
   152  			host: "github.com",
   153  			want: "https://api.github.com/",
   154  		},
   155  		{
   156  			host: "ghe.io",
   157  			want: "https://ghe.io/api/v3/",
   158  		},
   159  	}
   160  	for _, tt := range tests {
   161  		t.Run(tt.host, func(t *testing.T) {
   162  			if got := RESTPrefix(tt.host); got != tt.want {
   163  				t.Errorf("RESTPrefix() = %v, want %v", got, tt.want)
   164  			}
   165  		})
   166  	}
   167  }