blob: 1d4392db107477b4b966762e0e96b3af122a2c36 [file] [log] [blame]
package mock
import (
"fmt"
"net"
"net/http"
"sync"
"encoding/json"
"time"
"github.com/julienschmidt/httprouter"
"github.com/apid/istioApigeeAdapter/common"
)
var mockInit = &sync.Once{}
type MockServer struct {
listener net.Listener
}
func StartMockServer(port int) (*MockServer, error) {
mockInit.Do(makeKeys)
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
ms := &MockServer{
listener: l,
}
router := httprouter.New()
router.GET("/publicKey", ms.getPublicKey)
router.GET("/products", ms.getProducts)
go func() {
http.Serve(l, router)
}()
return ms, nil
}
func (m *MockServer) Address() string {
return m.listener.Addr().String()
}
func (m *MockServer) Stop() {
m.listener.Close()
}
func (m *MockServer) getPublicKey(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Header().Set("content-type", "text/plain")
w.Write(mockCertPEM)
}
func (m *MockServer) getProducts(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
now := time.Now()
p := []common.APIProduct{
{
Name: "First",
DisplayName: "First Product",
Description: "First test product",
CreatedAt: now.UnixNano(),
CreatedBy: "foo@bar.com",
LastModifiedAt: now.UnixNano(),
LastModifiedBy: "foo@bar.com",
APIResources: []string{"/**"},
ApprovalType: "auto",
Attributes: []common.Attribute{
{
Name: "access",
Value: "public",
},
},
Environments: []string{"test", "prod"},
Proxies: []string{"ProxyOne"},
Quota: "10",
QuotaInterval: "1",
QuotaTimeUnit: "minute",
},
}
w.Header().Set("content-type", "application/json")
enc := json.NewEncoder(w)
enc.Encode(p)
}