-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1cd93e
commit 298a670
Showing
5 changed files
with
119 additions
and
19 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: build | ||
on: | ||
push: | ||
branches: | ||
- '**' | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
# pull-requests: read | ||
|
||
jobs: | ||
goBuild: | ||
name: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.21' | ||
cache: false | ||
- name: tidy | ||
run: | | ||
go clean -modcache | ||
go mod tidy | ||
- name: server build | ||
run: go build server/main.go | ||
- name: client build | ||
run: | | ||
cd client | ||
make build-all | ||
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: test | ||
on: | ||
push: | ||
branches: | ||
- '**' | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
# pull-requests: read | ||
|
||
jobs: | ||
goTest: | ||
name: test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: 'stable' | ||
cache: false | ||
- name: test | ||
run: go test ./... -count=1 |
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,7 +1,65 @@ | ||
package senddata | ||
|
||
import "testing" | ||
import ( | ||
"bytes" | ||
"devashishRaj/rpi_telemetry/server/handleError" | ||
"encoding/json" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestHttpPost(t *testing.T) { | ||
// Mock server | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Verify request headers | ||
if r.Header.Get("Content-Type") != "application/json" { | ||
t.Errorf("Expected Content-Type header to be application/json, but got %s", r.Header.Get("Content-Type")) | ||
} | ||
|
||
// Verify request body | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
expectedJSON := []byte(`{"key":"value"}`) // Replace with your expected JSON | ||
if !bytes.Equal(body, expectedJSON) { | ||
t.Errorf("Expected request body to be %s, but got %s", expectedJSON, body) | ||
} | ||
|
||
// Send a dummy response | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write([]byte("OK")) | ||
handleError.CheckError("error in writing dummy responser", err) | ||
})) | ||
|
||
// Close the server when the test is done | ||
defer server.Close() | ||
|
||
// Redirect log output to a buffer for assertions | ||
var buf bytes.Buffer | ||
log.SetOutput(&buf) | ||
|
||
// Call the HttpPost function with the mock server URL and JSON data | ||
URL := server.URL | ||
jsonData := []byte(`{"key":"value"}`) // Replace with your actual JSON data | ||
|
||
// Call the function | ||
HttpPost(URL, jsonData) | ||
|
||
// Verify received JSON data | ||
var receivedData map[string]interface{} | ||
err := json.Unmarshal(jsonData, &receivedData) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Replace with your expected data | ||
expectedData := map[string]interface{}{"key": "value"} | ||
if !reflect.DeepEqual(receivedData, expectedData) { | ||
t.Errorf("Expected received JSON data to be %v, but got %v", expectedData, receivedData) | ||
} | ||
} |