github.com/google/go-github/v74@v74.0.0/github/repos_autolinks_test.go (about)

     1  // Copyright 2021 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  
    15  	"github.com/google/go-cmp/cmp"
    16  )
    17  
    18  func TestRepositoriesService_ListAutolinks(t *testing.T) {
    19  	t.Parallel()
    20  	client, mux, _ := setup(t)
    21  
    22  	mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testFormValues(t, r, values{"page": "2"})
    25  		fmt.Fprint(w, `[{"id":1, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=<num>"}, {"id":2, "key_prefix": "STORY-", "url_template": "https://example.com/STORY?query=<num>"}]`)
    26  	})
    27  
    28  	opt := &ListOptions{
    29  		Page: 2,
    30  	}
    31  	ctx := context.Background()
    32  	autolinks, _, err := client.Repositories.ListAutolinks(ctx, "o", "r", opt)
    33  	if err != nil {
    34  		t.Errorf("Repositories.ListAutolinks returned error: %v", err)
    35  	}
    36  
    37  	want := []*Autolink{
    38  		{ID: Ptr(int64(1)), KeyPrefix: Ptr("TICKET-"), URLTemplate: Ptr("https://example.com/TICKET?query=<num>")},
    39  		{ID: Ptr(int64(2)), KeyPrefix: Ptr("STORY-"), URLTemplate: Ptr("https://example.com/STORY?query=<num>")},
    40  	}
    41  
    42  	if !cmp.Equal(autolinks, want) {
    43  		t.Errorf("Repositories.ListAutolinks returned %+v, want %+v", autolinks, want)
    44  	}
    45  
    46  	const methodName = "ListAutolinks"
    47  	testBadOptions(t, methodName, func() (err error) {
    48  		_, _, err = client.Repositories.ListAutolinks(ctx, "\n", "\n", opt)
    49  		return err
    50  	})
    51  
    52  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    53  		got, resp, err := client.Repositories.ListAutolinks(ctx, "o", "r", opt)
    54  		if got != nil {
    55  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    56  		}
    57  		return resp, err
    58  	})
    59  }
    60  
    61  func TestRepositoriesService_AddAutolink(t *testing.T) {
    62  	t.Parallel()
    63  	client, mux, _ := setup(t)
    64  
    65  	opt := &AutolinkOptions{
    66  		KeyPrefix:      Ptr("TICKET-"),
    67  		URLTemplate:    Ptr("https://example.com/TICKET?query=<num>"),
    68  		IsAlphanumeric: Ptr(true),
    69  	}
    70  	mux.HandleFunc("/repos/o/r/autolinks", func(w http.ResponseWriter, r *http.Request) {
    71  		v := new(AutolinkOptions)
    72  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
    73  		testMethod(t, r, "POST")
    74  		if !cmp.Equal(v, opt) {
    75  			t.Errorf("Request body = %+v, want %+v", v, opt)
    76  		}
    77  		w.WriteHeader(http.StatusOK)
    78  		assertWrite(t, w, []byte(`
    79  			{
    80  				"key_prefix": "TICKET-",
    81  				"url_template": "https://example.com/TICKET?query=<num>",
    82  				"is_alphanumeric": true
    83  			}
    84  		`))
    85  	})
    86  	ctx := context.Background()
    87  	autolink, _, err := client.Repositories.AddAutolink(ctx, "o", "r", opt)
    88  	if err != nil {
    89  		t.Errorf("Repositories.AddAutolink returned error: %v", err)
    90  	}
    91  	want := &Autolink{
    92  		KeyPrefix:      Ptr("TICKET-"),
    93  		URLTemplate:    Ptr("https://example.com/TICKET?query=<num>"),
    94  		IsAlphanumeric: Ptr(true),
    95  	}
    96  
    97  	if !cmp.Equal(autolink, want) {
    98  		t.Errorf("AddAutolink returned %+v, want %+v", autolink, want)
    99  	}
   100  
   101  	const methodName = "AddAutolink"
   102  	testBadOptions(t, methodName, func() (err error) {
   103  		_, _, err = client.Repositories.AddAutolink(ctx, "\n", "\n", opt)
   104  		return err
   105  	})
   106  
   107  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   108  		got, resp, err := client.Repositories.AddAutolink(ctx, "o", "r", opt)
   109  		if got != nil {
   110  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   111  		}
   112  		return resp, err
   113  	})
   114  }
   115  
   116  func TestRepositoriesService_GetAutolink(t *testing.T) {
   117  	t.Parallel()
   118  	client, mux, _ := setup(t)
   119  
   120  	mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) {
   121  		testMethod(t, r, "GET")
   122  		fmt.Fprint(w, `{"id":1, "key_prefix": "TICKET-", "url_template": "https://example.com/TICKET?query=<num>"}`)
   123  	})
   124  
   125  	ctx := context.Background()
   126  	autolink, _, err := client.Repositories.GetAutolink(ctx, "o", "r", 1)
   127  	if err != nil {
   128  		t.Errorf("Repositories.GetAutolink returned error: %v", err)
   129  	}
   130  
   131  	want := &Autolink{ID: Ptr(int64(1)), KeyPrefix: Ptr("TICKET-"), URLTemplate: Ptr("https://example.com/TICKET?query=<num>")}
   132  	if !cmp.Equal(autolink, want) {
   133  		t.Errorf("Repositories.GetAutolink returned %+v, want %+v", autolink, want)
   134  	}
   135  
   136  	const methodName = "GetAutolink"
   137  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   138  		got, resp, err := client.Repositories.GetAutolink(ctx, "o", "r", 2)
   139  		if got != nil {
   140  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   141  		}
   142  		return resp, err
   143  	})
   144  }
   145  
   146  func TestRepositoriesService_DeleteAutolink(t *testing.T) {
   147  	t.Parallel()
   148  	client, mux, _ := setup(t)
   149  
   150  	mux.HandleFunc("/repos/o/r/autolinks/1", func(w http.ResponseWriter, r *http.Request) {
   151  		testMethod(t, r, "DELETE")
   152  		w.WriteHeader(http.StatusNoContent)
   153  	})
   154  
   155  	ctx := context.Background()
   156  	_, err := client.Repositories.DeleteAutolink(ctx, "o", "r", 1)
   157  	if err != nil {
   158  		t.Errorf("Repositories.DeleteAutolink returned error: %v", err)
   159  	}
   160  
   161  	const methodName = "DeleteAutolink"
   162  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   163  		return client.Repositories.DeleteAutolink(ctx, "o", "r", 2)
   164  	})
   165  }
   166  
   167  func TestAutolinkOptions_Marshal(t *testing.T) {
   168  	t.Parallel()
   169  	testJSONMarshal(t, &AutolinkOptions{}, "{}")
   170  
   171  	r := &AutolinkOptions{
   172  		KeyPrefix:      Ptr("kp"),
   173  		URLTemplate:    Ptr("URLT"),
   174  		IsAlphanumeric: Ptr(true),
   175  	}
   176  
   177  	want := `{
   178  		"key_prefix": "kp",
   179  		"url_template": "URLT",
   180  		"is_alphanumeric": true
   181  	}`
   182  
   183  	testJSONMarshal(t, r, want)
   184  }
   185  
   186  func TestAutolink_Marshal(t *testing.T) {
   187  	t.Parallel()
   188  	testJSONMarshal(t, &Autolink{}, "{}")
   189  
   190  	r := &Autolink{
   191  		ID:             Ptr(int64(1)),
   192  		KeyPrefix:      Ptr("kp"),
   193  		URLTemplate:    Ptr("URLT"),
   194  		IsAlphanumeric: Ptr(true),
   195  	}
   196  
   197  	want := `{
   198  		"id": 1,
   199  		"key_prefix": "kp",
   200  		"url_template": "URLT",
   201  		"is_alphanumeric": true
   202  	}`
   203  
   204  	testJSONMarshal(t, r, want)
   205  }