Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to ssclient #3

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
- name: Check
run: |
go mod tidy
git diff --name-only --exit-code || (echo "Please run 'go mod tidy'."; exit 1)
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)
}