github.com/google/go-github/v57@v57.0.0/github/dependency_graph_test.go (about) 1 // Copyright 2023 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 "fmt" 11 "net/http" 12 "testing" 13 "time" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestDependencyGraphService_GetSBOM(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/owner/repo/dependency-graph/sbom", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprint(w, `{ 25 "sbom":{ 26 "creationInfo":{ 27 "created":"2021-09-01T00:00:00Z" 28 }, 29 "name":"owner/repo", 30 "packages":[ 31 { 32 "name":"rubygems:rails", 33 "versionInfo":"1.0.0" 34 } 35 ] 36 } 37 }`) 38 }) 39 40 ctx := context.Background() 41 sbom, _, err := client.DependencyGraph.GetSBOM(ctx, "owner", "repo") 42 if err != nil { 43 t.Errorf("DependencyGraph.GetSBOM returned error: %v", err) 44 } 45 46 testTime := time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC) 47 want := &SBOM{ 48 &SBOMInfo{ 49 CreationInfo: &CreationInfo{ 50 Created: &Timestamp{testTime}, 51 }, 52 Name: String("owner/repo"), 53 Packages: []*RepoDependencies{ 54 { 55 Name: String("rubygems:rails"), 56 VersionInfo: String("1.0.0"), 57 }, 58 }, 59 }, 60 } 61 62 if !cmp.Equal(sbom, want) { 63 t.Errorf("DependencyGraph.GetSBOM returned %+v, want %+v", sbom, want) 64 } 65 66 const methodName = "GetSBOM" 67 testBadOptions(t, methodName, func() (err error) { 68 _, _, err = client.DependencyGraph.GetSBOM(ctx, "\n", "\n") 69 return err 70 }) 71 72 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 73 got, resp, err := client.DependencyGraph.GetSBOM(ctx, "owner", "repo") 74 if got != nil { 75 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 76 } 77 return resp, err 78 }) 79 }