-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hatamiarash7/unit-test
Unit test
- Loading branch information
Showing
9 changed files
with
715 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
name: "CodeQL" | ||
name: CodeQL | ||
|
||
on: | ||
push: | ||
branches: ["main"] | ||
branches: | ||
- main | ||
paths: | ||
- cmd | ||
- pkg | ||
- go.mod | ||
- go.sum | ||
- .github/workflows/codeql.yaml | ||
pull_request: | ||
branches: ["main"] | ||
schedule: | ||
- cron: "42 5 * * 1" | ||
branches: | ||
- main | ||
paths: | ||
- cmd | ||
- pkg | ||
- go.mod | ||
- go.sum | ||
- .github/workflows/codeql.yaml | ||
types: [opened, reopened, synchronize, ready_for_review] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
analyze: | ||
name: Analyze | ||
if: github.event_name != 'pull_request' || !github.event.pull_request.draft | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
contents: read | ||
security-events: write | ||
|
||
steps: | ||
- name: Checkout repository | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Initialize CodeQL | ||
uses: github/codeql-action/init@v2 | ||
with: | ||
languages: "go" | ||
|
||
- name: Build code | ||
- name: Build | ||
run: make cli | ||
|
||
- name: Perform CodeQL Analysis | ||
- name: Test | ||
run: go test -v -race ./... | ||
|
||
- name: CodeQL Analysis | ||
uses: github/codeql-action/analyze@v2 | ||
with: | ||
category: "/language:go" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
{ | ||
"cSpell.words": ["ANAME", "gcli", "gookit", "iodef", "issuewild", "TLSA"] | ||
"cSpell.words": [ | ||
"ANAME", | ||
"backoff", | ||
"gcli", | ||
"gookit", | ||
"iodef", | ||
"issuewild", | ||
"stretchr", | ||
"TLSA", | ||
"unmarshalling" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package arvancloud | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
// HTTP request multiplexer | ||
mux *http.ServeMux | ||
|
||
// API client for test | ||
client *API | ||
|
||
// HTTP server used for mocking | ||
server *httptest.Server | ||
) | ||
|
||
func setup(opts ...Option) { | ||
// Create a test server | ||
mux = http.NewServeMux() | ||
server = httptest.NewServer(mux) | ||
|
||
// Disable rate limits and retries for test purpose | ||
opts = append([]Option{UsingRateLimit(100000), UsingRetryPolicy(0, 0, 0)}, opts...) | ||
|
||
// Configure client | ||
client, _ = New("deadbeef", opts...) | ||
client.BaseURL = server.URL | ||
} | ||
|
||
func teardown() { | ||
server.Close() | ||
} | ||
|
||
func TestHeaders(t *testing.T) { | ||
// Default | ||
setup() | ||
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
}) | ||
teardown() | ||
|
||
// Override defaults | ||
headers := make(http.Header) | ||
headers.Set("Content-Type", "application/graphql") | ||
headers.Add("H1", "V1") | ||
headers.Add("H2", "V2") | ||
setup(Headers(headers)) | ||
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) | ||
assert.Equal(t, "application/graphql", r.Header.Get("Content-Type")) | ||
assert.Equal(t, "V1", r.Header.Get("H1")) | ||
assert.Equal(t, "V2", r.Header.Get("H2")) | ||
}) | ||
teardown() | ||
|
||
// Authentication | ||
setup() | ||
client, err := New("TEST_TOKEN") | ||
assert.NoError(t, err) | ||
client.BaseURL = server.URL | ||
mux.HandleFunc("/domains/"+testDomain+"/dns-records", func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) | ||
assert.Equal(t, "Bearer TEST_TOKEN", r.Header.Get("Authorization")) | ||
assert.Equal(t, "application/json", r.Header.Get("Content-Type")) | ||
}) | ||
teardown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package arvancloud | ||
|
||
const ( | ||
// Testing | ||
testDomain = "test.ir" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.