# Path: via/resources.py
# class QueryURLResource(_URLResource):
#     """Resource for routes expecting urls from the query."""
# 
#     def url_from_query(self):
#         """Get the 'url' parameter from the query.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
#         try:
#             url = self._request.params["url"].strip()
#         except KeyError as err:
#             raise HTTPBadRequest("Required parameter 'url' missing") from err
# 
#         if not url:
#             raise HTTPBadRequest("Required parameter 'url' is blank")
# 
#         return self._normalise_url(url)
# 
# Path: via/views/view_pdf.py
# @view_config(route_name="proxy_google_drive_file", decorator=(has_secure_url_token,))
# @view_config(
#     route_name="proxy_google_drive_file:resource_key", decorator=(has_secure_url_token,)
# )
# def proxy_google_drive_file(request):
#     """Proxy a file from Google Drive."""
# 
#     # Add an iterable to stream the content instead of holding it all in memory
#     content_iterable = request.find_service(GoogleDriveAPI).iter_file(
#         file_id=request.matchdict["file_id"],
#         resource_key=request.matchdict.get("resource_key"),
#     )
# 
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(route_name="proxy_jstor_pdf", decorator=(has_secure_url_token,))
# def proxy_jstor_pdf(context, request):
#     jstor_api = request.find_service(JSTORAPI)
#     if not jstor_api.enabled:
#         raise HTTPUnauthorized("JSTOR integration not enabled in Via")
# 
#     content_iterable = jstor_api.jstor_pdf_stream(
#         context.url_from_query(), request.params["jstor.ip"]
#     )
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(route_name="proxy_onedrive_pdf", decorator=(has_secure_url_token,))
# def proxy_onedrive_pdf(context, request):
#     url = context.url_from_query()
# 
#     content_iterable = request.find_service(HTTPService).stream(
#         url, headers=add_request_headers({})
#     )
# 
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(
#     renderer="via:templates/pdf_viewer.html.jinja2",
#     route_name="view_pdf",
#     # We have to keep the leash short here for caching so we can pick up new
#     # immutable assets when they are deployed
#     http_cache=0,
#     decorator=(has_secure_url_token,),
# )
# def view_pdf(context, request):
#     """HTML page with client and the PDF embedded."""
# 
#     url = context.url_from_query()
#     request.checkmate.raise_if_blocked(url)
# 
#     _, h_config = Configuration.extract_from_params(request.params)
# 
#     return {
#         # The upstream PDF URL that should be associated with any annotations.
#         "pdf_url": url,
#         # The CORS-proxied PDF URL which the viewer should actually load the
#         # PDF from.
#         "proxy_pdf_url": request.find_service(PDFURLBuilder).get_pdf_url(url),
#         "client_embed_url": request.registry.settings["client_embed_url"],
#         "static_url": request.static_url,
#         "hypothesis_config": h_config,
#     }


from unittest.mock import sentinel
from h_matchers import Any
from pyramid.httpexceptions import HTTPNoContent, HTTPUnauthorized
from via.resources import QueryURLResource
from via.views.view_pdf import (
    proxy_google_drive_file,
    proxy_jstor_pdf,
    proxy_onedrive_pdf,
    view_pdf,
)
import pytest

        response = call_view("https://one-drive.com", view=proxy_onedrive_pdf)

        assert isinstance(response, HTTPNoContent)


@pytest.mark.usefixtures("secure_link_service", "pdf_url_builder_service")
class TestJSTORPDF:
    @pytest.mark.usefixtures("http_service")
    def test_it(self, call_view, jstor_api, pyramid_request):
        response = call_view(
            "jstor://DOI", view=proxy_jstor_pdf, params={"jstor.ip": "1.1.1.1"}
        )

        assert response.status_code == 200
        assert response.headers["Content-Disposition"] == "inline"
        assert response.headers["Content-Type"] == "application/pdf"
        assert (
            response.headers["Cache-Control"]
            == "public, max-age=43200, stale-while-revalidate=86400"
        )
        jstor_api.jstor_pdf_stream.assert_called_once_with(
            "jstor://DOI", pyramid_request.params["jstor.ip"]
        )

    def test_when_not_enabled(self, call_view, jstor_api):
        jstor_api.enabled = False

        with pytest.raises(HTTPUnauthorized):
            call_view(

# Path: via/resources.py
# class QueryURLResource(_URLResource):
#     """Resource for routes expecting urls from the query."""
# 
#     def url_from_query(self):
#         """Get the 'url' parameter from the query.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
#         try:
#             url = self._request.params["url"].strip()
#         except KeyError as err:
#             raise HTTPBadRequest("Required parameter 'url' missing") from err
# 
#         if not url:
#             raise HTTPBadRequest("Required parameter 'url' is blank")
# 
#         return self._normalise_url(url)
# 
# Path: via/views/view_pdf.py
# @view_config(route_name="proxy_google_drive_file", decorator=(has_secure_url_token,))
# @view_config(
#     route_name="proxy_google_drive_file:resource_key", decorator=(has_secure_url_token,)
# )
# def proxy_google_drive_file(request):
#     """Proxy a file from Google Drive."""
# 
#     # Add an iterable to stream the content instead of holding it all in memory
#     content_iterable = request.find_service(GoogleDriveAPI).iter_file(
#         file_id=request.matchdict["file_id"],
#         resource_key=request.matchdict.get("resource_key"),
#     )
# 
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(route_name="proxy_jstor_pdf", decorator=(has_secure_url_token,))
# def proxy_jstor_pdf(context, request):
#     jstor_api = request.find_service(JSTORAPI)
#     if not jstor_api.enabled:
#         raise HTTPUnauthorized("JSTOR integration not enabled in Via")
# 
#     content_iterable = jstor_api.jstor_pdf_stream(
#         context.url_from_query(), request.params["jstor.ip"]
#     )
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(route_name="proxy_onedrive_pdf", decorator=(has_secure_url_token,))
# def proxy_onedrive_pdf(context, request):
#     url = context.url_from_query()
# 
#     content_iterable = request.find_service(HTTPService).stream(
#         url, headers=add_request_headers({})
#     )
# 
#     return _iter_pdf_response(request.response, content_iterable)
# 
# @view_config(
#     renderer="via:templates/pdf_viewer.html.jinja2",
#     route_name="view_pdf",
#     # We have to keep the leash short here for caching so we can pick up new
#     # immutable assets when they are deployed
#     http_cache=0,
#     decorator=(has_secure_url_token,),
# )
# def view_pdf(context, request):
#     """HTML page with client and the PDF embedded."""
# 
#     url = context.url_from_query()
#     request.checkmate.raise_if_blocked(url)
# 
#     _, h_config = Configuration.extract_from_params(request.params)
# 
#     return {
#         # The upstream PDF URL that should be associated with any annotations.
#         "pdf_url": url,
#         # The CORS-proxied PDF URL which the viewer should actually load the
#         # PDF from.
#         "proxy_pdf_url": request.find_service(PDFURLBuilder).get_pdf_url(url),
#         "client_embed_url": request.registry.settings["client_embed_url"],
#         "static_url": request.static_url,
#         "hypothesis_config": h_config,
#     }


# Path: tests/unit/via/views/view_pdf_test.py
from unittest.mock import sentinel
from h_matchers import Any
from pyramid.httpexceptions import HTTPNoContent, HTTPUnauthorized
from via.resources import QueryURLResource
from via.views.view_pdf import (
    proxy_google_drive_file,
    proxy_jstor_pdf,
    proxy_onedrive_pdf,
    view_pdf,
)
import pytest


        response = call_view("https://one-drive.com", view=proxy_onedrive_pdf)

        assert isinstance(response, HTTPNoContent)


@pytest.mark.usefixtures("secure_link_service", "pdf_url_builder_service")
class TestJSTORPDF:
    @pytest.mark.usefixtures("http_service")
    def test_it(self, call_view, jstor_api, pyramid_request):
        response = call_view(
            "jstor://DOI", view=proxy_jstor_pdf, params={"jstor.ip": "1.1.1.1"}
        )

        assert response.status_code == 200
        assert response.headers["Content-Disposition"] == "inline"
        assert response.headers["Content-Type"] == "application/pdf"
        assert (
            response.headers["Cache-Control"]
            == "public, max-age=43200, stale-while-revalidate=86400"
        )
        jstor_api.jstor_pdf_stream.assert_called_once_with(
            "jstor://DOI", pyramid_request.params["jstor.ip"]
        )

    def test_when_not_enabled(self, call_view, jstor_api):
        jstor_api.enabled = False

        with pytest.raises(HTTPUnauthorized):
            call_view(

                "jstor://DOI", view=proxy_jstor_pdf, params={"jstor.ip": "1.1.1.1"}