示例#1
0
def test_headers(httpserver):
    name = 'my-token'
    value = 'topsecret'
    headers = {name: value}
    client = DICOMwebClient(httpserver.url, headers=headers)
    client.store_instances([])
    request = httpserver.requests[0]
    assert request.headers[name] == value
示例#2
0
def test_set_http_retry_params(httpserver, client):
    retry = True
    retriable_error_codes = (HTTPStatus.TOO_MANY_REQUESTS,
                             HTTPStatus.SERVICE_UNAVAILABLE)
    max_attempts = 10
    wait_exponential_multiplier = 100
    client = DICOMwebClient(httpserver.url)
    client.set_http_retry_params(retry, max_attempts,
                                 wait_exponential_multiplier,
                                 retriable_error_codes)
    assert client._http_retry == retry
    assert client._http_retrable_errors == retriable_error_codes
    assert client._max_attempts == max_attempts
    assert client._wait_exponential_multiplier == wait_exponential_multiplier
示例#3
0
def test_retrieve_instance_default_transfer_syntax(httpserver, client,
                                                   cache_dir):
    cache_filename = str(cache_dir.joinpath('file.dcm'))
    with open(cache_filename, 'rb') as f:
        data = f.read()
    media_type = 'application/dicom'
    boundary = 'boundary'
    headers = {
        'content-type': ('multipart/related; '
                         f'type="{media_type}"; '
                         f'boundary="{boundary}"'),
    }
    message = DICOMwebClient._encode_multipart_message(
        content=[data], content_type=headers['content-type'])
    httpserver.serve_content(content=message, code=200, headers=headers)
    study_instance_uid = '1.2.3'
    series_instance_uid = '1.2.4'
    sop_instance_uid = '1.2.5'
    client.retrieve_instance(study_instance_uid,
                             series_instance_uid,
                             sop_instance_uid,
                             media_types=((
                                 'application/dicom',
                                 '1.2.840.10008.1.2.1',
                             ), ))
    request = httpserver.requests[0]
    assert request.accept_mimetypes[0][0][:43] == headers['content-type'][:43]
示例#4
0
def test_retrieve_instance(httpserver, client, cache_dir):
    cache_filename = str(cache_dir.joinpath('file.dcm'))
    with open(cache_filename, 'rb') as f:
        data = f.read()
    media_type = 'application/dicom'
    boundary = 'boundary'
    headers = {
        'content-type': ('multipart/related; '
                         f'type="{media_type}"; '
                         f'boundary="{boundary}"'),
    }
    message = DICOMwebClient._encode_multipart_message(
        content=[data], content_type=headers['content-type'])
    httpserver.serve_content(content=message, code=200, headers=headers)
    study_instance_uid = '1.2.3'
    series_instance_uid = '1.2.4'
    sop_instance_uid = '1.2.5'
    response = client.retrieve_instance(study_instance_uid,
                                        series_instance_uid, sop_instance_uid)
    with BytesIO() as fp:
        pydicom.dcmwrite(fp, response)
        raw_result = fp.getvalue()
    assert raw_result == data
    request = httpserver.requests[0]
    expected_path = (f'/studies/{study_instance_uid}'
                     f'/series/{series_instance_uid}'
                     f'/instances/{sop_instance_uid}')
    assert request.path == expected_path
    assert request.accept_mimetypes[0][0][:43] == headers['content-type'][:43]
示例#5
0
def test_url_prefixes(httpserver):
    wado_url_prefix = 'wado'
    qido_url_prefix = 'qido'
    stow_url_prefix = 'stow'
    client = DICOMwebClient(
        httpserver.url,
        wado_url_prefix=wado_url_prefix,
        qido_url_prefix=qido_url_prefix,
        stow_url_prefix=stow_url_prefix,
    )
    assert client.url_prefix == ''
    assert client.qido_url_prefix == qido_url_prefix
    assert client.wado_url_prefix == wado_url_prefix
    assert client.stow_url_prefix == stow_url_prefix
示例#6
0
def test_url(httpserver):
    protocol = 'http'
    host = 'localhost'
    port = 8080
    path = '/dcm4chee-arc/aets/DCM4CHEE/rs'
    url = '{protocol}://{host}:{port}{path}'.format(protocol=protocol,
                                                    host=host,
                                                    port=port,
                                                    path=path)
    client = DICOMwebClient(url)
    assert client.protocol == protocol
    assert client.host == host
    assert client.port == port
    assert client.url_prefix == path
    assert client.qido_url_prefix is None
    assert client.wado_url_prefix is None
    assert client.stow_url_prefix is None
示例#7
0
def test_iter_instance_frames_jpeg(httpserver, client, cache_dir):
    cache_filename = str(cache_dir.joinpath('retrieve_instance_pixeldata.jpg'))
    with open(cache_filename, 'rb') as f:
        data = f.read()

    n_resources = 2
    chunk_size = 10**2
    media_type = 'image/jpeg'
    boundary = 'boundary'
    headers = {
        'content-type': ('multipart/related; '
                         f'type="{media_type}"; '
                         f'boundary="{boundary}"'),
        'transfer-encoding':
        'chunked'
    }
    message = DICOMwebClient._encode_multipart_message(
        content=[data for _ in range(n_resources)],
        content_type=headers['content-type'])
    chunked_message = _chunk_message(message, chunk_size)
    httpserver.serve_content(content=chunked_message,
                             code=200,
                             headers=headers)
    study_instance_uid = '1.2.3'
    series_instance_uid = '1.2.4'
    sop_instance_uid = '1.2.5'
    frame_numbers = [x + 1 for x in range(n_resources)]
    frame_list = ','.join([str(n) for n in frame_numbers])
    iterator = client.iter_instance_frames(study_instance_uid,
                                           series_instance_uid,
                                           sop_instance_uid,
                                           frame_numbers,
                                           media_types=(media_type, ))
    response = list(iterator)
    request = httpserver.requests[0]
    expected_path = (f'/studies/{study_instance_uid}'
                     f'/series/{series_instance_uid}'
                     f'/instances/{sop_instance_uid}'
                     f'/frames/{frame_list}')
    assert isinstance(iterator, Generator)
    assert request.path == expected_path
    assert request.accept_mimetypes[0][0][:36] == headers['content-type'][:36]
    assert len(response) == n_resources
示例#8
0
def test_iter_series(client, httpserver, cache_dir):
    cache_filename = str(cache_dir.joinpath('file.dcm'))
    with open(cache_filename, 'rb') as f:
        data = f.read()

    n_resources = 3
    chunk_size = 10**3
    media_type = 'application/dicom'
    boundary = 'boundary'
    headers = {
        'content-type': ('multipart/related; '
                         f'type="{media_type}"; '
                         f'boundary="{boundary}"'),
        'transfer-encoding':
        'chunked'
    }

    message = DICOMwebClient._encode_multipart_message(
        content=[data for _ in range(n_resources)],
        content_type=headers['content-type'])
    chunked_message = _chunk_message(message, chunk_size)

    httpserver.serve_content(content=chunked_message,
                             code=200,
                             headers=headers)
    study_uid = '1.2.3'
    series_uid = '1.2.4'
    iterator = client.iter_series(study_uid, series_uid)
    assert isinstance(iterator, Generator)
    response = list(iterator)
    for instance in response:
        with BytesIO() as fp:
            pydicom.dcmwrite(fp, instance)
            raw_result = fp.getvalue()
        assert raw_result == data
    request = httpserver.requests[0]
    assert request.path == f'/studies/{study_uid}/series/{series_uid}'
    assert request.accept_mimetypes[0][0][:43] == headers['content-type'][:43]
    assert len(response) == n_resources
示例#9
0
def test_proxies(httpserver):
    protocol = 'http'
    address = 'foo.com'
    proxies = {protocol: address}
    client = DICOMwebClient(httpserver.url, proxies=proxies)
    assert client._session.proxies[protocol] == address
示例#10
0
def test_content_extraction_from_part():
    part = b'xyz\r\n\r\n\x00\x01\x00\x02\x0d\x0a\x0d\x0a\x00\x01\x00\x02'
    content = DICOMwebClient._extract_part_content(part)
    assert len(content) == 12