private void ValidateCosistencyLevel(DocumentClient client) { DocumentCollection collection = TestCommon.CreateOrGetDocumentCollection(client); // Value not supported INameValueCollection headers = new StringKeyValueCollection(); headers.Add(HttpConstants.HttpHeaders.ConsistencyLevel, "Not a valid value"); try { ReadDocumentFeedRequest(client, collection.ResourceId, headers); Assert.Fail("Should throw an exception"); } catch (Exception ex) { var innerException = ex.InnerException as DocumentClientException; Assert.IsTrue(innerException.StatusCode == HttpStatusCode.BadRequest, "invalid status code"); } // Supported value headers = new StringKeyValueCollection(); headers.Add(HttpConstants.HttpHeaders.ConsistencyLevel, ConsistencyLevel.Eventual.ToString()); var response = ReadDocumentFeedRequest(client, collection.ResourceId, headers); Assert.IsTrue(response.StatusCode == HttpStatusCode.OK, "Invalid status code"); }
private DocumentServiceResponse CreateDocumentRequest(DocumentClient client, INameValueCollection headers) { var collection = TestCommon.CreateOrGetDocumentCollection(client); var document = new Document() { Id = Guid.NewGuid().ToString() }; DocumentServiceRequest request = DocumentServiceRequest.Create(OperationType.Create, collection.SelfLink, document, ResourceType.Document, AuthorizationTokenType.Invalid, headers, SerializationFormattingPolicy.None); request.Headers[HttpConstants.HttpHeaders.PartitionKey] = PartitionKeyInternal.Empty.ToJsonString(); var response = client.CreateAsync(request).Result; return(response); }
private StoredProcedureResponse <string> CreateDocumentScript(DocumentClient client, INameValueCollection headers) { var headersIterator = headers.AllKeys().SelectMany(headers.GetValues, (k, v) => new { key = k, value = v }); var scriptOptions = "{"; var headerIndex = 0; foreach (var header in headersIterator) { if (headerIndex != 0) { scriptOptions += ", "; } headerIndex++; scriptOptions += header.key + ":" + header.value; } scriptOptions += "}"; var guid = Guid.NewGuid().ToString(); var script = @" function() { var client = getContext().getCollection(); client.createDocument(client.getSelfLink(), { id: Math.random() + """" }," + scriptOptions + @", function(err, docCreated, options) { if(err) throw new Error('Error while creating document: ' + err.message); else { getContext().getResponse().setBody(JSON.stringify(docCreated)); } });}"; var collection = TestCommon.CreateOrGetDocumentCollection(client); var sproc = new StoredProcedure() { Id = Guid.NewGuid().ToString(), Body = script }; var createdSproc = client.CreateStoredProcedureAsync(collection, sproc).Result.Resource; var result = client.ExecuteStoredProcedureAsync <string>(createdSproc).Result; return(result); }
private StoredProcedureResponse <string> ReadFeedScript(DocumentClient client, INameValueCollection headers) { var headersIterator = headers.AllKeys().SelectMany(headers.GetValues, (k, v) => new { key = k, value = v }); var scriptOptions = "{"; var headerIndex = 0; foreach (var header in headersIterator) { if (headerIndex != 0) { scriptOptions += ", "; } headerIndex++; scriptOptions += header.key + ":" + header.value; } scriptOptions += "}"; var script = @"function() { var client = getContext().getCollection(); function callback(err, docFeed, responseOptions) { if(err) throw 'Error while reading documents'; docFeed.forEach(function(doc, i, arr) { getContext().getResponse().appendBody(JSON.stringify(doc)); }); }; client.readDocuments(client.getSelfLink()," + scriptOptions + @", callback);}"; var collection = TestCommon.CreateOrGetDocumentCollection(client); var sproc = new StoredProcedure() { Id = Guid.NewGuid().ToString(), Body = script }; var createdSproc = client.CreateStoredProcedureAsync(collection, sproc).Result.Resource; var result = client.ExecuteStoredProcedureAsync <string>(createdSproc).Result; return(result); }