Skip to content

Commit 452f80e

Browse files
authored
tests: removed undue dependencies in test code for scanner (#3257)
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent c7fab0a commit 452f80e

2 files changed

Lines changed: 44 additions & 29 deletions

File tree

fixtures/bugs/3125/full/api/api.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
package api
44

55
import (
6+
"encoding/json"
7+
"fmt"
68
"net/http"
7-
8-
"github.com/labstack/echo/v4"
9+
"strconv"
910
)
1011

1112
type User struct {
@@ -31,13 +32,34 @@ type FooBarResponse struct {
3132
}
3233

3334
// FooBarHandler handles incoming foobar requests
34-
func FooBarHandler(ctx echo.Context) error {
35-
req := FooBarRequest{}
36-
if err := ctx.Bind(&req); err != nil {
37-
return echo.ErrBadRequest
35+
func FooBarHandler(w http.ResponseWriter, req *http.Request) {
36+
if err := req.ParseForm(); err != nil {
37+
http.Error(w, fmt.Sprintf("%s: %v", http.StatusText(http.StatusBadRequest), err), http.StatusBadRequest)
38+
return
39+
}
40+
raw := req.FormValue("age")
41+
age, err := strconv.Atoi(raw)
42+
if err != nil {
43+
http.Error(w, fmt.Sprintf("%s: %v", http.StatusText(http.StatusBadRequest), err), http.StatusBadRequest)
44+
return
45+
}
46+
47+
r := FooBarRequest{
48+
Foo: req.FormValue("foo"),
49+
User: User{
50+
Name: req.FormValue("name"),
51+
Age: age,
52+
},
53+
}
54+
55+
resp := doSthWithRequest(r)
56+
57+
enc := json.NewEncoder(w)
58+
err = enc.Encode(resp)
59+
if err != nil {
60+
http.Error(w, fmt.Sprintf("%s: %v", http.StatusText(http.StatusInternalServerError), err), http.StatusInternalServerError)
61+
return
3862
}
39-
resp := doSthWithRequest(req)
40-
return ctx.JSON(http.StatusOK, resp)
4163
}
4264

4365
func doSthWithRequest(req FooBarRequest) FooBarResponse {

fixtures/bugs/3125/full/main.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,25 @@
33
package main
44

55
import (
6-
"swagger/api"
6+
"log"
7+
"net"
8+
"net/http"
79

8-
"github.com/labstack/echo/v4"
9-
"github.com/labstack/echo/v4/middleware"
10+
"swagger/api"
1011
)
1112

1213
func main() {
13-
// Echo instance
14-
e := echo.New()
15-
16-
e.Use(middleware.Logger())
17-
e.Use(middleware.Recover())
18-
19-
// Set up basic auth with username=foo and password=bar
20-
e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{
21-
Validator: func(username, password string, c echo.Context) (bool, error) {
22-
if username == "foo" && password == "bar" {
23-
return true, nil
24-
}
25-
return false, nil
26-
},
27-
}))
28-
2914
// Route => handler
30-
e.POST("/foobar", api.FooBarHandler)
15+
http.HandleFunc("POST /foobar", api.FooBarHandler)
3116

3217
// Start server
33-
e.Logger.Fatal(e.Start(":1323"))
18+
listener, err := net.Listen("tcp", ":1323")
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
err = http.Serve(listener, nil)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
3427
}

0 commit comments

Comments
 (0)