/
service.go
98 lines (80 loc) · 2.58 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package fake
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/kyma-project/kyma/components/cms-services/pkg/runtime/service"
log "github.com/sirupsen/logrus"
)
// Service is a fake implementation of the Asset Store service.
type Service struct {
endpoints []service.HTTPEndpoint
mux *http.ServeMux
}
var _ service.Service = &Service{}
// NewService is a constructor that creates a new fake service.
func NewService() *Service {
return &Service{}
}
// RequestBodyFromFile builds a multipart request from a file.
func RequestBodyFromFile(filePath, parameters string) (io.Reader, string, error) {
buffer := &bytes.Buffer{}
formWriter := multipart.NewWriter(buffer)
defer formWriter.Close()
if filePath != "" {
file, err := os.Open(filePath)
if err != nil {
return nil, "", errors.Wrapf(err, "while opening the file %s", filePath)
}
defer file.Close()
contentWriter, err := formWriter.CreateFormFile("content", filepath.Base(file.Name()))
if err != nil {
return nil, "", errors.Wrapf(err, "while creating the content field for the file %s", filePath)
}
_, err = io.Copy(contentWriter, file)
if err != nil {
return nil, "", errors.Wrapf(err, "while copying the file %s to the content field", filePath)
}
}
if parameters != "" {
err := formWriter.WriteField("parameters", parameters)
if err != nil {
return nil, "", errors.Wrapf(err, "while creating the parameters field for parameters %s", parameters)
}
}
return buffer, formWriter.FormDataContentType(), nil
}
// ServeHTTP dispatches the request to the handler that
// most closely matches the request URL in its pattern.
func (s *Service) ServeHTTP(method, endpoint, contentType string, body io.Reader) *http.Response {
recorder := httptest.NewRecorder()
if s.mux == nil {
http.Error(recorder, "Server is not initialized", http.StatusInternalServerError)
}
request := httptest.NewRequest(method, endpoint, body)
request.Header.Add("Content-Type", contentType)
s.mux.ServeHTTP(recorder, request)
return recorder.Result()
}
// Start configures routes in the fake service.
func (s *Service) Start(ctx context.Context) error {
mux := http.NewServeMux()
for _, endpoint := range s.endpoints {
log.Infof("Registering %s endpoint", endpoint.Name())
path := fmt.Sprintf("/%s", endpoint.Name())
mux.HandleFunc(path, endpoint.Handle)
}
s.mux = mux
return nil
}
// Register adds endpoints to the service.
func (s *Service) Register(endpoint service.HTTPEndpoint) {
s.endpoints = append(s.endpoints, endpoint)
}