-
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.
added logic to setup content-type if it is a POST request, simple tes…
…t file
- Loading branch information
daniel rivera
authored and
daniel rivera
committed
Apr 29, 2024
1 parent
eb24cc6
commit e65da41
Showing
5 changed files
with
223 additions
and
18 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,9 @@ | ||
package common | ||
|
||
import "errors" | ||
|
||
// Common errors | ||
var ( | ||
ErrorTimeout = errors.New("Timeout") | ||
ErrorRefused = errors.New("Refused") | ||
) |
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
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,82 @@ | ||
package main | ||
|
||
import ( | ||
"devoteam-load-generator/common" | ||
"devoteam-load-generator/internal/worker" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func TestWorker(t *testing.T) { | ||
// Define expected response data | ||
expectedData := worker.OutputHttpWorker{ | ||
StatusCode: 200, | ||
} | ||
//expectedData := []byte(`{"message": "success"}`) | ||
jsonData, err := json.Marshal(expectedData) | ||
|
||
fmt.Println(jsonData) | ||
|
||
if err != nil { | ||
t.Errorf("Error marshalling mock response: %v", err) | ||
return | ||
} | ||
// Create a Gin router for mocking | ||
ginRouter := gin.Default() | ||
|
||
// Define a handler for the expected URL and method | ||
ginRouter.GET("/", func(c *gin.Context) { | ||
|
||
c.Writer.Header().Set("Content-Type", "application/json") | ||
c.Writer.Write(jsonData) | ||
c.Writer.WriteHeader(http.StatusOK) | ||
|
||
}) | ||
|
||
ginRouter.POST("/cart", func(c *gin.Context) { | ||
|
||
c.Writer.Header().Set("Content-Type", "application/json") | ||
c.Writer.Write(jsonData) | ||
c.Writer.WriteHeader(http.StatusOK) | ||
|
||
}) | ||
|
||
// Create a server using the Gin router | ||
ts := httptest.NewServer(ginRouter) | ||
defer ts.Close() | ||
|
||
// define HttpWorker | ||
client := &http.Client{} | ||
testGetWorker := worker.HttpWorker{ | ||
Url: ts.URL, | ||
HttpMethod: "GET", | ||
HttpClient: client, | ||
} | ||
|
||
testPostWorker := worker.HttpWorker{ | ||
Url: ts.URL, | ||
HttpMethod: "POST", | ||
HttpClient: client, | ||
} | ||
// Update Worker function to use the test server URL | ||
_, err = testGetWorker.Run() | ||
|
||
_, err2 := testPostWorker.Run() | ||
|
||
// Assertions | ||
// compare if error is different to nil or ERROR | ||
|
||
if err != nil && err != common.ErrorTimeout { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
if err2 != nil && err2 != common.ErrorTimeout { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
} |