github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/service/bitbucket/server_api_context_test.go (about)

     1  package bitbucket
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	insights "github.com/reva2/bitbucket-insights-api"
     8  )
     9  
    10  func TestWithServerVariables(t *testing.T) {
    11  	serverTests := []struct {
    12  		url             string
    13  		protocol        string
    14  		bitbucketDomain string
    15  	}{
    16  		{"http://bitbucket.host.tld", "http", "bitbucket.host.tld"},
    17  		{"https://host.tld", "https", "host.tld"},
    18  		{"http://host.tld/bitbucket", "http", "host.tld/bitbucket"},
    19  		{"https://host.tld/bit/bu/cket", "https", "host.tld/bit/bu/cket"},
    20  		{"http://localhost:7990", "http", "localhost:7990"},
    21  		{"https://localhost:7990/bb", "https", "localhost:7990/bb"},
    22  	}
    23  
    24  	for _, server := range serverTests {
    25  		// given
    26  		ctx := context.Background()
    27  
    28  		t.Run(server.url, func(t *testing.T) {
    29  			// when
    30  			resCtx, err := withServerVariables(ctx, server.url)
    31  
    32  			// then
    33  			if err != nil {
    34  				t.Fatalf("valid url must not cause error")
    35  			}
    36  
    37  			serverVariables := resCtx.Value(insights.ContextServerVariables)
    38  			if serverVariables == nil {
    39  				t.Fatalf("serverVariables must not be nil")
    40  			}
    41  
    42  			actualProtocol := serverVariables.(map[string]string)["protocol"]
    43  			if actualProtocol != server.protocol {
    44  				t.Fatalf("want %s, but got %s", server.protocol, actualProtocol)
    45  			}
    46  
    47  			actualDomain := serverVariables.(map[string]string)["bitbucketDomain"]
    48  			if actualDomain != server.bitbucketDomain {
    49  				t.Fatalf("want %s, but got %s", server.bitbucketDomain, actualDomain)
    50  			}
    51  		})
    52  	}
    53  
    54  	wrongServerTests := []string{
    55  		":::",
    56  		"http//bitbucket.my-company.com",
    57  		"http::/bitbucket.my-company.com",
    58  	}
    59  
    60  	for _, server := range wrongServerTests {
    61  		// given
    62  		ctx := context.Background()
    63  
    64  		t.Run("fail to parse "+server, func(t *testing.T) {
    65  			// when
    66  			resCtx, err := withServerVariables(ctx, server)
    67  
    68  			if err == nil {
    69  				t.Fatalf("expect parsing to fail for url: %s, but got %s", server, resCtx.Value(insights.ContextServerVariables))
    70  			}
    71  		})
    72  	}
    73  }