// MockDragonflyProvider mocks a Dragonfly server.
MockDragonflyProvider mocks a Dragonfly server.
// MockDragonflyProvider mocks a Dragonfly server .
func MockDragonflyProvider() *httptest.Server {
	return httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.RequestURI {
		case healthCheckEndpoint:
			if r.Method != http.MethodGet {
				w.WriteHeader(http.StatusNotImplemented)
				return
			}
			w.WriteHeader(http.StatusOK)
		case preheatEndpoint:
			if r.Method != http.MethodPost {
				w.WriteHeader(http.StatusNotImplemented)
				return
			}
			data, err := ioutil.ReadAll(r.Body)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				_, _ = w.Write([]byte(err.Error()))
				return
			}
			image := &PreheatImage{}
			if err := json.Unmarshal(data, image); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				_, _ = w.Write([]byte(err.Error()))
				return
			}
			if image.Type == "image" &&
				image.URL == "https://harbor.com" &&
				image.ImageName == "busybox" &&
				image.Tag == "latest" {
				w.WriteHeader(http.StatusOK)
				_, _ = w.Write([]byte(`{"ID":"dragonfly-id"}`))
				return
			}
			w.WriteHeader(http.StatusBadRequest)
		case strings.Replace(preheatTaskEndpoint, "{task_id}", "dragonfly-id", 1):
			if r.Method != http.MethodGet {
				w.WriteHeader(http.StatusNotImplemented)
				return
			}
			status := &dragonflyPreheatInfo{
				ID:         "dragonfly-id",
				StartTime:  time.Now().UTC().String(),
				FinishTime: time.Now().Add(5 * time.Minute).UTC().String(),
				Status:     "SUCCESS",
			}
			bytes, _ := json.Marshal(status)
			_, _ = w.Write(bytes)
		default:
			w.WriteHeader(http.StatusNotImplemented)
		}
	}))
}
func MockDragonflyProvider ( ) * httptest . Server { return httptest . NewUnstartedServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) { switch r . RequestURI { case healthCheckEndpoint : if r . Method != http . MethodGet { w . WriteHeader ( http . StatusNotImplemented ) return } w . WriteHeader ( http . StatusOK ) case preheatEndpoint : if r . Method != http . MethodPost { w . WriteHeader ( http . StatusNotImplemented ) return } data , err := ioutil . ReadAll ( r . Body ) if err != nil { w . WriteHeader ( http . StatusInternalServerError ) _ , _ = w . Write ( [ ] byte ( err . Error ( ) ) ) return } image := & PreheatImage { } if err := json . Unmarshal ( data , image ) ; err != nil { w . WriteHeader ( http . StatusInternalServerError ) _ , _ = w . Write ( [ ] byte ( err . Error ( ) ) ) return } if image . Type == " " && image . URL == " " && image . ImageName == " " && image . Tag == " " { w . WriteHeader ( http . StatusOK ) _ , _ = w . Write ( [ ] byte ( `{"ID":"dragonfly-id"}` ) ) return } w . WriteHeader ( http . StatusBadRequest ) case strings . Replace ( preheatTaskEndpoint , " " , " " , 1 ) : if r . Method != http . MethodGet { w . WriteHeader ( http . StatusNotImplemented ) return } status := & dragonflyPreheatInfo { ID : " " , StartTime : time . Now ( ) . UTC ( ) . String ( ) , FinishTime : time . Now ( ) . Add ( 5 * time . Minute ) . UTC ( ) . String ( ) , Status : " " , } bytes , _ := json . Marshal ( status ) _ , _ = w . Write ( bytes ) default : w . WriteHeader ( http . StatusNotImplemented ) } } ) ) }
MockDragonflyProvider mocks a Dragonfly server.
MockDragonflyProvider mocks a Dragonfly server .