Skip to content

Commit

Permalink
update test and workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 14, 2023
1 parent c3cda2a commit 01a7cd5
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -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.
```
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
```
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
2 changes: 1 addition & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
50 changes: 50 additions & 0 deletions instance_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
12 changes: 12 additions & 0 deletions utils/base_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions utils/json_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 01a7cd5

Please sign in to comment.