Skip to content

Commit

Permalink
Add tests to ssclient
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed May 11, 2024
1 parent 2896145 commit ded9a09
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests
on:
pull_request:
push:
permissions:
contents: read
jobs:
test:
name: Test
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
- name: Run tests
run: go test -race ./...
mod:
name: Check that `go mod tidy` is clean
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version-file: hack/ccp/go.mod
cache-dependency-path: hack/ccp/go.sum
- name: Check
run: |
go mod tidy
git diff --name-only --exit-code || (echo "Please run 'go mod tidy'."; exit 1)
working-directory: hack/ccp
64 changes: 64 additions & 0 deletions ssclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ssclient_test

import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"testing"

kiotahttpgo "github.com/microsoft/kiota-http-go"

"go.artefactual.dev/ssclient"
)

func assertEqual(t *testing.T, got, want interface{}) {
if !reflect.DeepEqual(got, want) {
t.Errorf("Mismatch found:\nGot: %#v\nWant: %#v", got, want)
}
}

func TestClient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.URL.Path, "/api/v2/location")
assertEqual(t, r.Header, http.Header(map[string][]string{
"Accept": {"application/json"},
"Accept-Encoding": {"gzip"},
"Authorization": {"ApiKey test:test"},
"User-Agent": {"kiota-go/" + kiotahttpgo.NewUserAgentHandlerOptions().ProductVersion},
}))

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [
{
"enabled": true,
"path": "/home",
"pipeline": ["/api/v2/pipeline/a64e061a-5688-49b5-95c1-0b6885c40c04/"],
"purpose": "DS",
"quota": null,
"relative_path": "home",
"resource_uri": "/api/v2/location/fff70864-a5d4-4ca6-ab29-b4ce67d8eeab/",
"space": "/api/v2/space/218caeb7-fd59-4b7b-99b1-f5771a2dd34f/",
"uuid": "fff70864-a5d4-4ca6-ab29-b4ce67d8eeab"
}
]
}`))
}))
t.Cleanup(func() { srv.Close() })

client, err := ssclient.New(&http.Client{}, srv.URL, "test", "test")
assertEqual(t, err, nil)

locations, err := client.Api().V2().Location().Get(context.Background(), nil)
assertEqual(t, err, nil)
assertEqual(t, len(locations.GetObjects()), 1)
}

0 comments on commit ded9a09

Please sign in to comment.