From 01a7cd52b52a66695edec9e481bfd382cddf970c Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Sun, 15 Oct 2023 06:54:51 +0800 Subject: [PATCH] update test and workflow --- .github/ISSUE_TEMPLATE.md | 15 +++++++++++ .github/workflows/test.yaml | 19 ++++++++++++++ README.md | 14 ++++++++++- go.mod | 7 ++++-- go.sum | 5 ++++ instance.go | 2 +- instance_test.go | 50 +++++++++++++++++++++++++++++++++++++ utils/base_test.go | 12 +++++++++ utils/json_test.go | 20 +++++++++++++++ 9 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/workflows/test.yaml create mode 100644 instance_test.go create mode 100644 utils/base_test.go create mode 100644 utils/json_test.go diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..653f503 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,15 @@ +* chatnio version: +* Go version: +* Operating System: + +### Description + +Describe what you were trying to get done. +Tell us what happened, what went wrong, and what you expected to happen. + +### What I Did + +``` +Paste the command(s) you ran and the output. +If there was a crash, please include the traceback here. +``` \ No newline at end of file diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..4cfedc8 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,19 @@ +name: Test +on: + push: + branches: + - main +jobs: + release: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Golang + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Test + run: go test diff --git a/README.md b/README.md index 3eaf407..c353823 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,20 @@ # ChatNio Go Library ---- The official Go library for the Chat Nio API - Authors: Deeptrain Team - Free software: MIT license - Documentation: https://docs.chatnio.net + +## Features + +- Chat +- Conversation +- Quota +- Subscription and Package + +## Installation + +```shell +go get -u github.com/deeptrain-community/chatnio-api-go +``` diff --git a/go.mod b/go.mod index d092a28..8ff04b3 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,13 @@ module github.com/Deeptrain-Community/chatnio-api-go go 1.20 +require ( + github.com/gorilla/websocket v1.5.0 + github.com/valyala/fasthttp v1.50.0 +) + require ( github.com/andybalholm/brotli v1.0.5 // indirect - github.com/gorilla/websocket v1.5.0 // indirect github.com/klauspost/compress v1.17.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.50.0 // indirect ) diff --git a/go.sum b/go.sum index e6b89a6..7011e23 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,10 @@ +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2gPS5g= github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M= github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= diff --git a/instance.go b/instance.go index b386b61..52f6e09 100644 --- a/instance.go +++ b/instance.go @@ -46,7 +46,7 @@ func (i *Instance) IsAuthenticated() bool { } func (i *Instance) GetChatEndpoint() string { - host := utils.TrimPrefixes(i.Endpoint, "http://", "https://") + host := utils.TrimPrefixes(i.GetEndpoint(), "http://", "https://") return fmt.Sprintf("wss://%s/chat", host) } diff --git a/instance_test.go b/instance_test.go new file mode 100644 index 0000000..ef92de8 --- /dev/null +++ b/instance_test.go @@ -0,0 +1,50 @@ +package chatnio + +import ( + "testing" +) + +var instance *Instance + +func init() { + instance = NewInstanceFromEnv("CHATNIO_TOKEN") +} + +func TestInstance_GetEndpoint(t *testing.T) { + if instance.GetEndpoint() != "https://api.chatnio.net" { + t.Error("endpoint is not https://api.chatnio.net") + } +} + +func TestInstance_GetChatEndpoint(t *testing.T) { + if instance.GetChatEndpoint() != "wss://api.chatnio.net/chat" { + t.Error("chat endpoint is not correct") + } +} + +func TestInstance_GetHeaders(t *testing.T) { + headers := instance.GetHeaders() + if headers["Authorization"] != "Bearer "+instance.GetApiKey() { + t.Error("authorization header is not correct") + } +} + +func TestInstance_Mix(t *testing.T) { + if instance.Mix("/test") != "https://api.chatnio.net/test" { + t.Error("mix is not correct") + } +} + +func TestInstance_IsAuthenticated(t *testing.T) { + if len(instance.ApiKey) > 0 && !instance.IsAuthenticated() { + t.Error("authentication is not correct") + } +} + +func TestInstance_GetChatApiKey(t *testing.T) { + if instance.IsAuthenticated() && instance.GetChatApiKey() != instance.ApiKey { + t.Error("chat api key is not correct") + } else if !instance.IsAuthenticated() && instance.GetChatApiKey() != "anonymous" { + t.Error("chat api key is not correct") + } +} diff --git a/utils/base_test.go b/utils/base_test.go new file mode 100644 index 0000000..ad1c840 --- /dev/null +++ b/utils/base_test.go @@ -0,0 +1,12 @@ +package utils + +import "testing" + +func TestTrimPrefixes(t *testing.T) { + data := "https://api.chatnio.net" + expected := "api.chatnio.net" + actual := TrimPrefixes(data, "http://", "https://") + if actual != expected { + t.Errorf("expected %s, got %s", expected, actual) + } +} diff --git a/utils/json_test.go b/utils/json_test.go new file mode 100644 index 0000000..49ffdc0 --- /dev/null +++ b/utils/json_test.go @@ -0,0 +1,20 @@ +package utils + +import ( + "testing" +) + +func TestMarshalForm(t *testing.T) { + data := map[string]string{ + "foo": "bar", + } + res := MarshalForm(data) + if res == "" { + t.Error("marshal form failed") + } + + rev := UnmarshalForm[map[string]string](res) + if rev == nil || (*rev)["foo"] != "bar" { + t.Error("form is invalid") + } +}