...

Source file src/github.com/google/go-github/v55/github/git_blobs_test.go

Documentation: github.com/google/go-github/v55/github

     1  // Copyright 2014 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  	"bytes"
    10  	"context"
    11  	"encoding/json"
    12  	"fmt"
    13  	"net/http"
    14  	"testing"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestGitService_GetBlob(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  
    26  		fmt.Fprint(w, `{
    27  			  "sha": "s",
    28  			  "content": "blob content"
    29  			}`)
    30  	})
    31  
    32  	ctx := context.Background()
    33  	blob, _, err := client.Git.GetBlob(ctx, "o", "r", "s")
    34  	if err != nil {
    35  		t.Errorf("Git.GetBlob returned error: %v", err)
    36  	}
    37  
    38  	want := Blob{
    39  		SHA:     String("s"),
    40  		Content: String("blob content"),
    41  	}
    42  
    43  	if !cmp.Equal(*blob, want) {
    44  		t.Errorf("Blob.Get returned %+v, want %+v", *blob, want)
    45  	}
    46  
    47  	const methodName = "GetBlob"
    48  	testBadOptions(t, methodName, func() (err error) {
    49  		_, _, err = client.Git.GetBlob(ctx, "\n", "\n", "\n")
    50  		return err
    51  	})
    52  
    53  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    54  		got, resp, err := client.Git.GetBlob(ctx, "o", "r", "s")
    55  		if got != nil {
    56  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    57  		}
    58  		return resp, err
    59  	})
    60  }
    61  
    62  func TestGitService_GetBlob_invalidOwner(t *testing.T) {
    63  	client, _, _, teardown := setup()
    64  	defer teardown()
    65  
    66  	ctx := context.Background()
    67  	_, _, err := client.Git.GetBlob(ctx, "%", "%", "%")
    68  	testURLParseError(t, err)
    69  }
    70  
    71  func TestGitService_GetBlobRaw(t *testing.T) {
    72  	client, mux, _, teardown := setup()
    73  	defer teardown()
    74  
    75  	mux.HandleFunc("/repos/o/r/git/blobs/s", func(w http.ResponseWriter, r *http.Request) {
    76  		testMethod(t, r, "GET")
    77  		testHeader(t, r, "Accept", "application/vnd.github.v3.raw")
    78  
    79  		fmt.Fprint(w, `raw contents here`)
    80  	})
    81  
    82  	ctx := context.Background()
    83  	blob, _, err := client.Git.GetBlobRaw(ctx, "o", "r", "s")
    84  	if err != nil {
    85  		t.Errorf("Git.GetBlobRaw returned error: %v", err)
    86  	}
    87  
    88  	want := []byte("raw contents here")
    89  	if !bytes.Equal(blob, want) {
    90  		t.Errorf("GetBlobRaw returned %q, want %q", blob, want)
    91  	}
    92  
    93  	const methodName = "GetBlobRaw"
    94  	testBadOptions(t, methodName, func() (err error) {
    95  		_, _, err = client.Git.GetBlobRaw(ctx, "\n", "\n", "\n")
    96  		return err
    97  	})
    98  
    99  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   100  		got, resp, err := client.Git.GetBlobRaw(ctx, "o", "r", "s")
   101  		if got != nil {
   102  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   103  		}
   104  		return resp, err
   105  	})
   106  }
   107  
   108  func TestGitService_CreateBlob(t *testing.T) {
   109  	client, mux, _, teardown := setup()
   110  	defer teardown()
   111  
   112  	input := &Blob{
   113  		SHA:      String("s"),
   114  		Content:  String("blob content"),
   115  		Encoding: String("utf-8"),
   116  		Size:     Int(12),
   117  	}
   118  
   119  	mux.HandleFunc("/repos/o/r/git/blobs", func(w http.ResponseWriter, r *http.Request) {
   120  		v := new(Blob)
   121  		json.NewDecoder(r.Body).Decode(v)
   122  
   123  		testMethod(t, r, "POST")
   124  
   125  		want := input
   126  		if !cmp.Equal(v, want) {
   127  			t.Errorf("Git.CreateBlob request body: %+v, want %+v", v, want)
   128  		}
   129  
   130  		fmt.Fprint(w, `{
   131  		 "sha": "s",
   132  		 "content": "blob content",
   133  		 "encoding": "utf-8",
   134  		 "size": 12
   135  		}`)
   136  	})
   137  
   138  	ctx := context.Background()
   139  	blob, _, err := client.Git.CreateBlob(ctx, "o", "r", input)
   140  	if err != nil {
   141  		t.Errorf("Git.CreateBlob returned error: %v", err)
   142  	}
   143  
   144  	want := input
   145  
   146  	if !cmp.Equal(*blob, *want) {
   147  		t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, *want)
   148  	}
   149  
   150  	const methodName = "CreateBlob"
   151  	testBadOptions(t, methodName, func() (err error) {
   152  		_, _, err = client.Git.CreateBlob(ctx, "\n", "\n", input)
   153  		return err
   154  	})
   155  
   156  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   157  		got, resp, err := client.Git.CreateBlob(ctx, "o", "r", input)
   158  		if got != nil {
   159  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   160  		}
   161  		return resp, err
   162  	})
   163  }
   164  
   165  func TestGitService_CreateBlob_invalidOwner(t *testing.T) {
   166  	client, _, _, teardown := setup()
   167  	defer teardown()
   168  
   169  	ctx := context.Background()
   170  	_, _, err := client.Git.CreateBlob(ctx, "%", "%", &Blob{})
   171  	testURLParseError(t, err)
   172  }
   173  
   174  func TestBlob_Marshal(t *testing.T) {
   175  	testJSONMarshal(t, &Blob{}, "{}")
   176  
   177  	u := &Blob{
   178  		Content:  String("content"),
   179  		Encoding: String("encoding"),
   180  		SHA:      String("sha"),
   181  		Size:     Int(1),
   182  		URL:      String("url"),
   183  		NodeID:   String("nid"),
   184  	}
   185  
   186  	want := `{
   187  		"content": "content",
   188  		"encoding": "encoding",
   189  		"sha": "sha",
   190  		"size": 1,
   191  		"url": "url",
   192  		"node_id": "nid"
   193  	}`
   194  
   195  	testJSONMarshal(t, u, want)
   196  }
   197  

View as plain text