Handler-ы находятся в другом пакете.Тестировать можно в том же пакете, но в целом это не важно, если только вы не делаете их приватными.
И вот вопросом в том, требуется ли мне дополнительная функция, которая бы возвращала *gin.Engine, чтобы я мог в тестововм файле запускать спокойно сервер
func testHTTPResponse(t *testing.T, r *gin.Engine, req *http.Request, f func(w *httptest.ResponseRecorder) bool) {
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if !f(w) {
t.Fail()
}
}
func TestApiHandler(t *testing.T) {
var a apiService
router := gin.Default()
router.GET("/api/login",a.SignIn)
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
testHTTPResponse(t, router, req, func(w *httptest.ResponseRecorder) bool {
statusOK := w.Code == http.StatusOK
p, err := ioutil.ReadAll(w.Body)
pageOK := err == nil && strings.Index(string(p), "<title>Register</title>") > 0
return statusOK && pageOK
})
}
mock.ExpectExec(`INSERT INTO Products \(model, company, price\) values \('\$1', \$2, \$3\)`).
WithArgs(tt.args.model, tt.args.company, tt.args.price).
WillReturnResult(sqlmock.NewResult(1, 1))
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-http-deployment
labels:
app: http-http
spec:
replicas: 1
selector:
matchLabels:
app: http-http
template:
metadata:
labels:
app: http-http
spec:
containers:
- name: http-http
image: stasbigunenko/http_http
env:
- name: PORT_HTTP
value: ":8085"
- name: GRPC
value: "grpc-service:9000"
ports:
- containerPort: 8085
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-grpc-deployment
labels:
app: http-grpc
spec:
replicas: 1
selector:
matchLabels:
app: http-grpc
template:
metadata:
labels:
app: http-grpc
spec:
containers:
- name: http-grpc
image: stasbigunenko/http_grpc
env:
- name: TCP_PORT
value: ":9000"
ports:
- containerPort: 9000
---
apiVersion: v1
kind: Service
metadata:
name: http-service
spec:
selector:
app: http-http
ports:
- protocol: TCP
port: 8085
---
apiVersion: v1
kind: Service
metadata:
name: grpc-service
spec:
selector:
app: http-grpc
ports:
- protocol: TCP
port: 9000
package main
import "testing"
func Test_sum(t *testing.T) {
tt := []struct {
args GetArgs
sum int
}{
{Args {2, 4}, 6},
{Args {2, -2}, 0},
}
for _, tc := range tt {
tci := tc.args.(GetArgs)
s := Sum(tci)
if s != tc.sum {
t.Errorf("sum of %v and %v should be %v, received- %v", tc.args.GetA(), tc.args.GetB(), tc.sum, s)
}
}
}
https://domain.com/products/chairs?height=200&width=100
r.HandleFunc("/products/{category_handle}", h.ShowProducts).Methods("GET")
// из извлекаем из URL категорию
vars := mux.Vars(r)
categoryHandle := vars["categoryHandle"]
// получаем параметры
height, _ := strconv.Atoi(r.URL.Query().Get("height"))
width, _ := strconv.Atoi(r.URL.Query().Get("width"))
package main
import (
"fmt"
"time"
)
var c = make(chan int, 3)
var data = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func main() {
fmt.Println("Hello, playground")
go save()
go read()
time.Sleep(3 * time.Second)
}
func save() {
for _, val := range data {
c <- val
}
}
func read() {
for {
val := <-c
fmt.Println("read:", val)
}
}
package main
import (
"fmt"
"time"
)
var c = make(chan *int)
var data = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func main() {
fmt.Println("Hello, playground")
go save()
go read()
time.Sleep(3 * time.Second)
}
func save() {
for _, val := range data {
v := val
c <- &v
}
}
func read() {
for {
val := <-c
fmt.Println("read:", *val)
}
}
package main
import (
"fmt"
"time"
var c = make(chan *int, 5)
var data = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func main() {
fmt.Println("Hello, playground")
go save()
go read()
time.Sleep(3 * time.Second)
}
func save() {
for i := range data {
c <- &data[i]
}
}
func read() {
for {
val := <-c
fmt.Println("read:", *val)
}
}
package main
import (
"fmt"
"time"
)
var c = make(chan *int, 5)
var data = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
func main() {
fmt.Println("Hello, playground")
go save()
go read()
time.Sleep(3 * time.Second)
}
func save() {
for _, val := range data {
c <- &val
fmt.Printf("write: %v\n", &val)
}
}
func read() {
for {
val := <-c
fmt.Println("read:", *val)
}
}
Hello, playground
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
write: 0xc000094000
...
func save() {
for _, val := range data {
v := val
c <- &v
fmt.Printf("write: %v\n", &v)
}
}
Hello, playground
write: 0xc000094000
write: 0xc000094010
write: 0xc000094018
write: 0xc000094020
write: 0xc000094028
write: 0xc000094030