github.com/osievert/jfrog-cli-core@v1.2.7/missioncontrol/utils/missioncontrolutils_test.go (about)

     1  package utils
     2  
     3  import "testing"
     4  
     5  func TestReadMissionControlHttpMessage(t *testing.T) {
     6  	// Test simple error message
     7  	resp := []byte("{\"errors\":[{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"}]}")
     8  	expected := "HTTP 404 Not Found"
     9  	errorMessage := ReadMissionControlHttpMessage(resp)
    10  	if expected != errorMessage {
    11  		t.Error("Unexpected error message. Expected: " + expected + " Got " + errorMessage)
    12  	}
    13  	// Test multiple messages
    14  	resp = []byte("{\"errors\":[{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"},{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"}]}")
    15  	expected = "HTTP 404 Not Found, HTTP 404 Not Found"
    16  	errorMessage = ReadMissionControlHttpMessage(resp)
    17  	if expected != errorMessage {
    18  		t.Error("Unexpected error message. Expected: " + expected + " Got " + errorMessage)
    19  	}
    20  	// Test none error response
    21  	resp = []byte("{\"data\":[{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"},{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"}]}")
    22  	expected = "{\"data\":[{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"},{\"message\":\"HTTP 404 Not Found\",\"type\":\"Exception\"}]}"
    23  	errorMessage = ReadMissionControlHttpMessage(resp)
    24  	if expected != errorMessage {
    25  		t.Error("Unexpected error message. Expected: " + expected + " Got " + errorMessage)
    26  	}
    27  	// Test response with details
    28  	resp = []byte("{\"errors\": [{\"message\": \"Validation constraint violation\",\"details\": [\"addInstance.req.url property must be a valid URL. Invalid value: 'the'\"],\"type\": \"Validation\"}]}")
    29  	expected = "Validation constraint violation addInstance.req.url property must be a valid URL. Invalid value: 'the'"
    30  	errorMessage = ReadMissionControlHttpMessage(resp)
    31  	if expected != errorMessage {
    32  		t.Error("Unexpected error message. Expected: \n" + expected + "\n Got \n" + errorMessage)
    33  	}
    34  	resp = []byte("{\"errors\": [{\"message\": \"Validation constraint violation\",\"details\": [\"addInstance.req.url property must be a valid URL. Invalid value: 'the'\" , \"test\"],\"type\": \"Validation\"}]}")
    35  	expected = "Validation constraint violation addInstance.req.url property must be a valid URL. Invalid value: 'the' test"
    36  	errorMessage = ReadMissionControlHttpMessage(resp)
    37  	if expected != errorMessage {
    38  		t.Error("Unexpected error message. Expected: \n" + expected + "\n Got \n" + errorMessage)
    39  	}
    40  }